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.

適用対象

こちらもご覧ください