我设计了一个在datagridview上显示颜色方案的表单(备用行)。工作正常。但是当我从mdi父窗体菜单条标签中调用它时,它在datagridview上没有显示颜色(填写表单加载函数)。当我只运行子窗体时,它在gridview3和datagridview4.like上显示颜色
但是当我从父母调用时,它不会在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();
}
答案 0 :(得分:1)
假设您已发布ChildForm的代码,并且您的应用程序已设置为MDI父级,并且您有数据。您应该使用DataGridViews的OnBindingComplete事件来调用gridrowcolor();从那里。这应该有用。
Form1_Load()
{
dataGridView4.OnBindingComplete += SetGridViewRows;
... // the rest of your code...
}
SetGridViewRows(object sender, BindingCompleteEventArgs e)
{
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;
}
}
上面的代码 - 我为了简洁而复制 - 你实际上可以使用同一个事件 - 只为你的目的使用发送者和eventargs。 使用CellFormatEvent - 我不建议,因为该方法可能会被调用两次,具体取决于你正在做什么。 使用DataGridView的BindingCompleteEvent可以解决问题。
修改 好的,我为你编写了代码:
Form1_Load()
{
// This method subscribes to the DataGridView Binding Complete Event. Only after DdataBinding is Complete
// For example when you do this dataGridView3.DataSource = MySource; or dataGridView3.ResetBindings(false);
dataGridView3.DataBindingComplete += dataGridView3_DataBindingComplete;
// This Method Subscribes to the Cell Formatting event - will be called when formatting the Cells!
dataGridView3.CellFormatting += new DataGridViewCellFormattingEventHandler(dataGridView3_CellFormatting);
// All of my other code that I have in the load event..
}
// This is called when Databinding is complete
private void dataGridView3_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
dgBinContent.ClearSelection();
DataGridView myGrid = (DataGridView)sender;
for (int i = 0; i < myGrid.Rows.Count - 1; i++)
{
myGrid.CellBorderStyle = DataGridViewCellBorderStyle.None;
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new System.Drawing.Font(myGrid.Font, System.Drawing.FontStyle.Bold);
int ii = Convert.ToInt32(myGrid.Rows[i].Cells[0].Value.ToString());
if (ii % 2 == 0)
{
myGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
myGrid.Rows[i].DefaultCellStyle = style;
myGrid.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Chocolate;
}
else
{
myGrid.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
}
}
}
// This is called when Cell Formatting
void dataGridView3_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
this.dataGridView3.Rows[e.RowIndex].HeaderCell.Value = String.Format("{0}", e.RowIndex + 1);
dataGridView3.CellBorderStyle = DataGridViewCellBorderStyle.None;
DataGridViewCellStyle style = new DataGridViewCellStyle();
style.Font = new System.Drawing.Font(dataGridView3.Font, System.Drawing.FontStyle.Bold);
int ii = Convert.ToInt32(dataGridView3.Rows[e.RowIndex].Cells[0].Value.ToString());
if (ii % 2 == 0)
{
dataGridView3.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Yellow;
dataGridView3.Rows[e.RowIndex].DefaultCellStyle = style;
dataGridView3.DefaultCellStyle.SelectionForeColor = System.Drawing.Color.Chocolate;
}
else
{
dataGridView3.Rows[e.RowIndex].DefaultCellStyle.BackColor = System.Drawing.Color.Orange;
}
}