How to run Run CellEndEdit only when Cell ValueCha

2020-03-23 17:41发布

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

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 & by fields 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 Value are same so couldn't put this in 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?

5条回答
Bombasti
2楼-- · 2020-03-23 17:43

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

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
查看更多
戒情不戒烟
3楼-- · 2020-03-23 17:57

I made it like so:

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:

 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

查看更多
▲ chillily
4楼-- · 2020-03-23 17:59

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 CellValidation trick.)

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.

查看更多
Emotional °昔
5楼-- · 2020-03-23 18:00
 MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
查看更多
Luminary・发光体
6楼-- · 2020-03-23 18:06

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.

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).

查看更多
登录 后发表回答