C# DataGridView CellFormatting事件未触发

C# DataGridView CellFormatting事件未触发,c#,.net,windows,winforms,C#,.net,Windows,Winforms,我为DataGridView上的CellFormatting事件添加了一个处理程序,用于根据行的内容修改背景颜色 即使数据被插入到表中,它似乎也不会触发。我通过双击IDE中的CellFormatting事件添加了事件处理程序,这似乎正确地创建了代码 private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // this neve

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

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

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

我可能做错了什么?

您可以尝试RowValidated事件:

 private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
 {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
 }

注意:单击行和关闭表单时将触发此事件。

您可以尝试RowValidated事件:

 private void dataGridView1_RowValidated(object sender, DataGridViewCellEventArgs e)
 {
        dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Blue;
 }

注意:当您单击行并关闭表单时,此事件将触发。

我认为您不能将CellFormating事件用于您的案例。当单元格内容需要格式化以供显示时,会发生此错误

请改为尝试CellValueChanged事件(http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)


中选择其他适当的事件。我认为您不能将CellFormating事件用于您的案例。当单元格内容需要格式化以供显示时,会发生此错误

请改为尝试CellValueChanged事件(http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.cellvaluechanged.aspx)


中选择其他适当的事件,我最终使用了RowsAdded事件,该事件捕获了我所需的内容。谢谢我最终使用了RowsAdded事件,它捕获了我需要的内容。谢谢