DataGridView CellFormatting事件未触发

时间:2011-04-20 20:55:42

标签: c# .net windows winforms

我在DataGridView上为CellFormatting事件添加了一个处理程序,以根据行的内容修改背景颜色。

即使将数据插入表中,它似乎也没有被触发。我在CellFormatting事件中通过双击来添加事件处理程序,这似乎正确地创建了代码。

   private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        // this never gets called
        MessageBox.Show("Event fired");
    }

我可能做错了什么?

2 个答案:

答案 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)

或者

http://msdn.microsoft.com/en-us/library/x4dwfh7x.aspx

中选择其他适当的事件
相关问题