This documentation is archived and is not being maintained.

DataGridView.CancelRowEdit Event

Occurs when the VirtualMode property of a DataGridView control is true and the cancels edits in a row.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

'Declaration
Public Event CancelRowEdit As QuestionEventHandler
'Usage
Dim instance As DataGridView 
Dim handler As QuestionEventHandler 

AddHandler instance.CancelRowEdit, handler

When the DataGridView is in virtual mode, changes are committed to the data cache at the cell level by default. The CancelRowEdit event can be used when implementing row-level transactions.

For more information about handling events, see Consuming Events.

The following code example illustrates how to handle this event for a DataGridView control in virtual mode. When the control is in edit mode, the rowInEdit variable holds the index of the row being edited, and the customerInEdit variable holds a reference to a Customer object corresponding to that row. When the user cancels out of edit mode, this object can be discarded. If the row the user was editing is the row for new records, however, the old Customer object is discarded and replaced with a new one so that the user can begin making edits again. This example is part of a larger example available in Walkthrough: Implementing Virtual Mode in the Windows Forms DataGridView Control.

Private Sub dataGridView1_CancelRowEdit(ByVal sender As Object, _
    ByVal e As System.Windows.Forms.QuestionEventArgs) _
    Handles dataGridView1.CancelRowEdit

    If Me.rowInEdit = Me.dataGridView1.Rows.Count - 2 AndAlso _
        Me.rowInEdit = Me.customers.Count Then 

        ' If the user has canceled the edit of a newly created row,  
        ' replace the corresponding Customer object with a new, empty one. 
        Me.customerInEdit = New Customer()

    Else 

        ' If the user has canceled the edit of an existing row,  
        ' release the corresponding Customer object. 
        Me.customerInEdit = Nothing 
        Me.rowInEdit = -1

    End If 

End Sub

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Show: