简体   繁体   English

当用户更改单元格值时捕获的 DataGridView 事件

[英]DataGridView Event to Catch When Cell Value Has Been Changed by User

I have a Winforms app written in C#.我有一个用 C# 编写的 Winforms 应用程序。

In one of my DataGridViews I have set all columns except one called 'Reference' to ReadOnly = true;在我的一个 DataGridViews 中,除了名为“Reference”的列之外,我已将所有列设置为 ReadOnly = true;

I want the application to know when a User has changed anything in the 'Reference' column, but all the events I have tried so far fire a lot more than when a user has made changes.我希望应用程序知道用户何时更改了“参考”列中的任何内容,但到目前为止我尝试过的所有事件都比用户进行更改时触发的事件多得多。 For example CurrentCellChanged fires when the DataGridView is initially rendered and everytime the user simply clicks or tabs along the rows etc.例如 CurrentCellChanged 在 DataGridView 最初呈现时触发,并且每次用户只需单击或沿行单击选项卡等。

I'm only interested in catching user changes to data in the 'Reference' column which is the ONLY column where ReadOnly = false;我只对捕获用户对“参考”列中数据的更改感兴趣,该列是唯一的列,其中 ReadOnly = false;

Which is the best event to use for this?哪个事件最适合用于此?

CellValueChanged is what you need: CellValueChanged正是您所需要的:

private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e){
  if(dataGridView1.Columns[e.ColumnIndex].Name == "Reference"){
    //your code goes here
  }
}

I think the event CellEndEdit is also suitable for your want:我认为事件CellEndEdit也适合您的需求:

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e){
  if(dataGridView1.Columns[e.ColumnIndex].Name == "Reference"){
    //your code goes here
  }
}

In my case CellValueChanged event was also triggering while the DGV was initializing, so I wanted to use CellEndEdit, as King King mentioned in his answer.在我的情况下,在 DGV 初始化时 CellValueChanged 事件也被触发,所以我想使用 CellEndEdit,正如 King King 在他的回答中提到的那样。

To make King King 's second answer more bullet-proof (see JPProgrammer 's comment), ie only react, if a value was entered in the cell, you can do the following:为了使King King的第二个答案更加防弹(请参阅JPProgrammer的评论),即仅反应,如果在单元格中输入了值,您可以执行以下操作:

   private void DataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
   {
        int? rowIdx = e?.RowIndex;
        int? colIdx = e?.ColumnIndex;
        if (rowIdx.HasValue && colIdx.HasValue)
        {
            var dgv = (DataGridView)sender;
            var cell = dgv?.Rows?[rowIdx.Value]?.Cells?[colIdx.Value]?.Value;
            if (!string.IsNullOrEmpty(cell?.ToString()))
            {
                // your code goes here
            };
        };
   }

Notice the null handling by using ?.注意使用?.的空处理?. and ?[ operators.?[运算符。 I have written it so that it can be used in a more general way, but of course you can add the check for the "Reference" column, just replace the inner if statement above by the following:我已经写了它,以便它可以以更通用的方式使用,但是当然您可以添加对“参考”列的检查,只需将上面的内部if语句替换为以下内容:

       if (dgv.Columns[colIdx.Value].Name == "Reference")
       {
          if (!string.IsNullOrEmpty(cell?.ToString()))
          {
               // your code goes here
          };
       };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM