【问题标题】:DataGridView CellFormatting Event Not FiringDataGridView CellFormatting 事件未触发
【发布时间】:2011-04-20 20:55:42
【问题描述】:

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

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

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

我做错了什么?

【问题讨论】:

    标签: c# .net windows winforms


    【解决方案1】:

    我认为您不能为您的案例使用 CellFormating 事件。当单元格的内容需要格式化以供显示时,就会发生这种情况。

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

    或者

    http://msdn.microsoft.com/en-us/library/x4dwfh7x.aspx中选择其他合适的事件

    【讨论】:

    • 我最终使用了 RowsAdded 事件,该事件捕获了我需要的内容。谢谢!
    【解决方案2】:

    你可以试试 RowValidated 事件:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多