DataGridView.UserDeletingRow DataGridView.UserDeletingRow DataGridView.UserDeletingRow DataGridView.UserDeletingRow Event

定义

在用户从 DataGridView 控件中删除行时发生。Occurs when the user deletes a row from the DataGridView control.

public:
 event System::Windows::Forms::DataGridViewRowCancelEventHandler ^ UserDeletingRow;
public event System.Windows.Forms.DataGridViewRowCancelEventHandler UserDeletingRow;
member this.UserDeletingRow : System.Windows.Forms.DataGridViewRowCancelEventHandler 
Public Custom Event UserDeletingRow As DataGridViewRowCancelEventHandler 

示例

下面的代码示例演示如何使用UserDeletingRow事件以取消删除的行从DataGridView如果选定内容中包括的起始余额行。The following code example demonstrates how to use the UserDeletingRow event to cancel the deletion of rows from the DataGridView if the starting balance row is included in the selection. 此示例摘自一个更大的示例中提供SelectionChanged事件。This example is part of a larger example available in the SelectionChanged event.

private void DataGridView1_UserDeletingRow(object sender,
    DataGridViewRowCancelEventArgs e)
{
    DataGridViewRow startingBalanceRow = DataGridView1.Rows[0];

    // Check if the Starting Balance row is included in the selected rows
    if (DataGridView1.SelectedRows.Contains(startingBalanceRow))
    {
        // Do not allow the user to delete the Starting Balance row.
        MessageBox.Show("Cannot delete Starting Balance row!");

        // Cancel the deletion if the Starting Balance row is included.
        e.Cancel = true;
    }
}
Private Sub UserDeletingRow(ByVal sender As Object, _
    ByVal e As DataGridViewRowCancelEventArgs) _
    Handles DataGridView1.UserDeletingRow

    Dim startingBalanceRow As DataGridViewRow = DataGridView1.Rows(0)

    ' Check if the starting balance row is included in the selected rows
    If DataGridView1.SelectedRows.Contains(startingBalanceRow) Then
        ' Do not allow the user to delete the Starting Balance row.
        MessageBox.Show("Cannot delete Starting Balance row!")

        ' Cancel the deletion if the Starting Balance row is included.
        e.Cancel = True
    End If
End Sub

注解

可以取消此事件,以防止行删除操作完成。This event can be canceled to prevent a row deletion from being completed.

有关如何处理事件的详细信息,请参阅处理和引发事件For more information about how to handle events, see Handling and Raising Events.

适用于

另请参阅