我的表单上有 DataGridView,我正在尝试为单元格着色。
这是代码:
public partial class Form1 : Form
{
DataTable dtToGrid = new DataTable();
BindingSource bs = new BindingSource();
public Form1()
{
InitializeComponent();
dtToGrid.Columns.Add("Group");
DataRow dr;
dr = dtToGrid.NewRow();
dr["Group"] = "must become lightGreen";
dtToGrid.Rows.Add(dr);
bs.DataSource = dtToGrid;
dataGridView1.DataSource = bs;
ColorData();
dataGridView1.Refresh();
}
public void ColorData()
{
dataGridView1[0, 0].Style.BackColor = Color.LightGreen;
}
private void button1_Click(object sender, EventArgs e)
{
ColorData();
}
}
如您所见,函数 ColorData 调用了两次,但是当它从构造函数调用时 - 没有任何反应并且单元格仍然是白色的。从 button1_Click 调用,它是事件函数,正确地为单元格着色。
怎么了?