C# 当用户更改单元格值时捕获的 DataGridView 事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19537784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridView Event to Catch When Cell Value Has Been Changed by User
提问by PJW
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?
哪个事件最适合用于此?
采纳答案by King King
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
}
}
回答by Matt
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), i.e. 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
};
};