我在DataGridView上为CellFormatting事件添加了一个处理程序,以根据行的内容修改背景颜色。
即使将数据插入表中,它似乎也没有被触发。我在CellFormatting事件中通过双击来添加事件处理程序,这似乎正确地创建了代码。
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
// this never gets called
MessageBox.Show("Event fired");
}
我可能做错了什么?
答案 0 :(得分:2)
您可以尝试RowValidated事件:
private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
}
注意:当您单击行并关闭表单时,将触发此事件。
答案 1 :(得分:2)
我认为你不能在你的情况下使用CellFormating事件。当需要格式化单元格的内容以进行显示时,会发生这种情况。
尝试使用CellValueChanged事件(http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)
或者
中选择其他适当的事件