【发布时间】:2015-12-10 23:27:07
【问题描述】:
我设计了一个表单,它在 datagridview(交替行)上显示配色方案。它工作正常。但是当我从 mdi 父表单菜单条选项卡中调用它时,它不会在 datagridview 上显示颜色(填充表单加载功能)。当我只运行子表单时,它会在 gridview3 和 datagridview4 上显示颜色。像
但是当我从父级调用时,它不会在 datagridview3(备用行)和 datagridview4(备用行)中显示颜色。 看起来像
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedItem = "Select Gender";
using (SqlConnection con = new SqlConnection(conn))
{
SqlDataAdapter sda = new SqlDataAdapter("getdata", con);
sda.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet ds = new DataSet();
sda.Fill(ds);
ds.Tables[0].TableName = "Product";
ds.Tables[1].TableName = "Category";
dataGridView3.DataSource = ds.Tables["Product"];
dataGridView4.DataSource = ds.Tables["Category"];
}
gridrowcolor();
}
public void gridrowcolor()
{
DataGridViewCellStyle st = new DataGridViewCellStyle();
st.Font = new Font("Arial", 12, FontStyle.Bold);
for (int i = 0; i < dataGridView4.Rows.Count-1; i++)
{
//dataGridView4.RowsDefaultCellStyle.BackColor = Color.Orange;
//dataGridView4.AlternatingRowsDefaultCellStyle.BackColor = Color.BurlyWood;
int ii = Convert.ToInt32(dataGridView4.Rows[i].Cells[0].Value.ToString());
if (ii % 2 == 0)
{
dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Gray;
dataGridView4.Rows[i].DefaultCellStyle = st;
}
else
dataGridView4.Rows[i].DefaultCellStyle.BackColor = Color.Brown;
}
for (int i = 0; i < dataGridView3.Rows.Count - 1; i++)
{
dataGridView3.CellBorderStyle = DataGridViewCellBorderStyle.None;
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new Font(dataGridView3.Font, FontStyle.Bold);
int ii = Convert.ToInt32(dataGridView3.Rows[i].Cells[0].Value.ToString());
if (ii % 2 == 0)
{
dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;
dataGridView3.Rows[i].DefaultCellStyle = style;
dataGridView3.DefaultCellStyle.SelectionForeColor = Color.Chocolate;
}
else
dataGridView3.Rows[i].DefaultCellStyle.BackColor = Color.Orange;
}
}
From Parent MDI Form
private void receiptCancelRequestToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.MdiParent = this;
frm.Show();
}
【问题讨论】: