C# 仅当DataGridView中的Cell ValueChanged时如何运行CellEndEdit

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8899329/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:03:38  来源:igfitidea点击:

How to run Run CellEndEdit only when Cell ValueChanged in DataGridView

c#.netwinformsvalidationdatagridview

提问by PUG

i want to run CellEndEdit only when value of cell is changed, tried putting

我只想在单元格的值更改时运行 CellEndEdit,尝试放置

if (dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == e.FormattedValue.ToString()) 
             return; 

in CellValidation event, the Cell Validation event does return but CellEndEdit also gets executed and updates, updated date& byfields when the user has only gone into edit mode and came out without changing the value cell.
By the time CellEndEdit is reached CellValue& Formatted Valueare same so couldn't put this in CellEndEdit.

在 CellValidation 事件中,Cell Validation 事件确实会返回,但 CellEndEdit 也会在用户仅进入编辑模式并在不更改值单元格的情况下退出时执行并更新updated date&by字段。
当到达时间CellEndEdit CellValueFormatted Value是相同的,所以不能把这个CellEndEdit。

A trivial solution is to set a flag in CellValidation and return CellEndEdit when flag is set, but this appears to be a error-prone solution as there are about 10 girds on the form. So 10 flags?

一个简单的解决方案是在 CellValidation 中设置一个标志并在设置标志时返回 CellEndEdit,但这似乎是一个容易出错的解决方案,因为表单上大约有 10 个网格。那么10个标志?

采纳答案by Aleksandar Vucetic

Instead of performing your tasks in CellEndEdit, put them in CellValueChanged. It is fired only when cell value is changed. Please note that it will fire when your DataGridViews are initially populated, but to handle that, you can put just one variable saying formInitialized or something, to make sure that you are not executing your CellEndEdit when you populate your data grids.

不要在 CellEndEdit 中执行您的任务,而是将它们放在 CellValueChanged 中。它仅在单元格值更改时触发。请注意,它会在您的 DataGridViews 最初填充时触发,但要处理该问题,您可以只放置一个变量表示 formInitialized 或其他内容,以确保您在填充数据网格时没有执行 CellEndEdit。

And to answer your question, there is no way to figure out if value is changed when CellEndEdit is fired, because it is always fired when cell gets out of edit mode. The only solution is, like you proposed, to store the old value externally, but you already noticed why is that bad (though it works really good in most of the cases).

并且为了回答您的问题,无法确定在 CellEndEdit 被触发时值是否更改,因为当单元格退出编辑模式时它总是被触发。唯一的解决方案是,就像您提出的那样,在外部存储旧值,但您已经注意到为什么那么糟糕(尽管在大多数情况下它确实很好用)。

回答by John Fisher

You could definitely get there by catching the current cell value in CellBeginEdit, then comparing it to the current cell value in CellEndEdit. (Or use your CellValidationtrick.)

您绝对可以通过捕获 中的当前单元格值CellBeginEdit,然后将其与 中的当前单元格值进行比较来实现CellEndEdit。(或者使用你的CellValidation技巧。)

To avoid "multiple flags", you could use a Dictionary<DataGridView,object>so you can key into the dictionary with the current event's grid, then get or set the appropriate value.

为了避免“多个标志”,您可以使用 aDictionary<DataGridView,object>以便您可以使用当前事件的网格键入字典,然后获取或设置适当的值。

回答by hadi.sh

 MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());

回答by Tof

But if you want to compute the value edited, you can use the suggested issue by J.Fisher like:

但是如果你想计算编辑的值,你可以使用 J.Fisher 建议的问题,如:

Private Sub dgvHost_CellBeginEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgvHost.CellBeginEdit
    dgvHost.CurrentCell.Tag = dgvHost.CurrentCell.Value
End Sub

Private Sub dgvHost_CellEndEdit(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHost.CellEndEdit
    If dgvHost.CurrentCell.Tag = dgvHost.CurrentCell.Value Then Exit Sub
    dgvHost.CurrentCell.Tag = Nothing
    'Do something like
    dgvHost.CurrentCell.Value = MD5(dgvHost.CurrentCell.Value)
End Sub

回答by Fusseldieb

I made it like so:

我是这样制作的:

C#:

C#:

private void DynList_RowValidated(object sender, DataGridViewCellEventArgs e)
{
    if (ChangedRow == true) {
        ChangedRow = false;
        //Row Changed...
    }

}
bool ChangedRow;
private void DynList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
    ChangedRow = true;
}

VB.Net:

VB.Net:

 Private Sub DynList_RowValidated(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        If ChangedRow = True Then
            ChangedRow = False
            'Row Changed...
        End If

 End Sub
 Dim ChangedRow As Boolean
 Private Sub DynList_CellValueChanged(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
        ChangedRow = True
 End Sub

I tried like 1 hour to archieve this, because no one had a solution for that, so I thought it might be useful for others

我尝试了 1 个小时来存档,因为没有人有解决方案,所以我认为它可能对其他人有用