Monday, October 19, 2015

DataGridView

DataGridView.RowEndEdit

This event raises when a user edits any cell in a row and leaves that row after editing. It doesn't raise, if a user leaves the row without editing any cell or user just ends the editing of that cell and move the focus to another cell in the same row.

How is RowEndEdit different than CellEndEdit?

CellEndEdit: It raises when a user edits any cell and ends the editing that cell.

RowEndEdit: It raises when a user edits any cell in a row and leave that row after editing.

Use Case

It is useful when you want to access complete row instead of a cell, like to insert or update a row in a table of a database.

Problem: No DataGridView.RowEndEdit event

Solution: First of all you have to check that if any cell in any row has been edited and set a flag for editing. To do this use


VB
Private Sub dgv_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgv.CellBeginEdit
    dgv.Rows(e.RowIndex).Tag = "Editing"
End Sub

C#
private void dgv_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e){
    dgv.Rows(e.RowIndex).Tag = "Editing";
}

After this use RowLeave event with check, if a user edited any cell of a row or not. To do this use

VB
Private Sub dgv_RowLeave(sender As Object, e As DataGridViewCellEventArgs) Handles dgv.RowLeave
    Dim dgvRow As DataGridViewRow = dgv.Rows(e.RowIndex)
    If dgvRow.Tag <> Nothing Then
        dgvRow.Tag = Nothing
            
        'Your code for RowEndEdit event goes here

    End If
End Sub

C#
private void dgv_RowLeave(object sender, DataGridViewCellEventArgs e){
    DataGridViewRow dgvRow = dgv.Rows(e.RowIndex)
    if (dgvRow.Tag != null){
        dgvRow.Tag = null

        'Your code for RowEndEdit event goes here

    }
}

No comments:

Post a Comment