DataGridView.UserDeletingRow 事件

定义

在用户从 DataGridView 控件中删除行时发生。

C#
public event System.Windows.Forms.DataGridViewRowCancelEventHandler UserDeletingRow;
C#
public event System.Windows.Forms.DataGridViewRowCancelEventHandler? UserDeletingRow;

事件类型

示例

下面的代码示例演示如何使用 UserDeletingRow 事件取消删除从 DataGridView 中的行(如果所选内容中包含起始余额行)。 此示例是 事件中提供的更大示例的 SelectionChanged 一部分。

C#
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;
    }
}

注解

可以取消此事件,以防止完成行删除。

有关如何处理事件的详细信息,请参阅 处理和引发事件

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅