DataGridView.UserDeletingRow Событие

Определение

Происходит, когда пользователь удаляет строку из элемента управления 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 

Тип события

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.

Применяется к