C# 子窗口窗体在gridview上不显示颜色方案
我设计了一个表单,在datagridview上显示颜色方案(交替行)。它工作得很好。但当我从mdi父窗体菜单条选项卡调用它时,它不会在datagridview(在窗体加载函数中填充)上显示颜色。当我运行独子窗体时,它会在gridview3和datagridview4.like上显示颜色 但当我从父级调用时,它在datagridview3(备用行)和datagridview4(备用行)中不显示颜色。 看起来像C# 子窗口窗体在gridview上不显示颜色方案,c#,winforms,gridview,C#,Winforms,Gridview,我设计了一个表单,在datagridview上显示颜色方案(交替行)。它工作得很好。但当我从mdi父窗体菜单条选项卡调用它时,它不会在datagridview(在窗体加载函数中填充)上显示颜色。当我运行独子窗体时,它会在gridview3和datagridview4.like上显示颜色 但当我从父级调用时,它在datagridview3(备用行)和datagridview4(备用行)中不显示颜色。 看起来像 private void Form1\u加载(对象发送方,事件参数e) { combo
private void Form1\u加载(对象发送方,事件参数e)
{
comboBox1.SelectedItem=“选择性别”;
使用(SqlConnection con=新SqlConnection(conn))
{
SqlDataAdapter sda=新的SqlDataAdapter(“getdata”,con);
sda.SelectCommand.CommandType=CommandType.StoredProcess;
数据集ds=新数据集();
sda.填充(ds);
ds.Tables[0].TableName=“产品”;
ds.Tables[1].TableName=“Category”;
dataGridView3.DataSource=ds.Tables[“产品”];
dataGridView4.DataSource=ds.Tables[“Category”];
}
gridrowcolor();
}
公共void gridrowcolor()
{
DataGridViewCellStyle st=新的DataGridViewCellStyle();
st.Font=新字体(“Arial”,12,FontStyle.Bold);
对于(int i=0;i
假设您已经发布了子表单的代码,并且您的应用程序设置为MDI parent,并且您有数据。您应该使用DataGridView的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;
}
}
Form1_Load()
{
dataGridView4.OnBindingComplete+=SetGridViewRows;
…//剩下的代码。。。
}
SetGridViewRows(对象发送器,BindingCompleteEventArgs e)
{
DataGridViewCellStyle st=新的DataGridViewCellStyle();
st.Font=新字体(“Arial”,12,FontStyle.Bold);
对于(int i=0;i
上面的代码(为了简洁起见,我复制了这些代码)实际上可以使用同一个事件(仅使用sender和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;
}
}
Form1_Load()
{
//此方法订阅DataGridView绑定完成事件。仅在DdataBinding完成后
//例如,执行此操作时,dataGridView3.DataSource=MySource;或dataGridView3.ResetBindings(false);
dataGridView3.DataBindingComplete+=dataGridView3_DataBindingComplete;
//此方法订阅单元格格式化事件-将在格式化单元格时调用!
dataGridView3.CellFormatting+=新的DataGridViewCellFormattingEventHandler(dataGridView3\u CellFormatting);
//加载事件中的所有其他代码。。
}
//当数据绑定完成时调用
私有void dataGridView3_DataBindingComplete(对象发送方,DataGridViewBindingCompleteEventTarget)
{
dgBinContent.ClearSelection();
DataGridView myGrid=(DataGridView)发送方;
对于(int i=0;iForm1_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;
}
}