How to run Run CellEndEdit only when Cell ValueChanged in DataGridView

I want to run CellEndEdit only when the cell value changes, I tried to put

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

in the CellValidation event, the Cell Validation event returns, but CellEndEdit also starts and updates the fields, updated dateand bywhen the user just went into edit mode and left without changing the cell values.
By the time they reach CellEndEdit, CellValuethey Formatted Valueare the same as they could not be installed in CellEndEdit.

The trivial solution is to set the flag in CellValidation and return CellEndEdit when the flag is set, but this seems to be an error-prone solution, since there are about 10 pointers in the form. So, 10 flags?

+5
source share
5 answers

Instead of completing your tasks in CellEndEdit, put them in CellValueChanged. It only starts when the cell value changes. Note that it will fire when your DataGridView is initially populated, but to process it, you can only put one variable saying formInitialized or something else to make sure that you do not execute your CellEndEdit when populating your data grids.

, , CellEndEdit, , . , , - , , ( ).

+13

, CellBeginEdit, CellEndEdit. ( CellValidation.)

" ", Dictionary<DataGridView,object>, , .

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

But if you want to calculate the editable value, you can use the proposed J.Fisher problem as:

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
0
source

I did it like this:

FROM#

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 1 hour to archive this because someone did not have a solution for this, so I thought it might be useful for others.

0
source

All Articles