DataGridView.UserDeletingRow 事件
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在用户从 DataGridView 控件中删除行时发生。
public:
event System::Windows::Forms::DataGridViewRowCancelEventHandler ^ UserDeletingRow;
C#
public event System.Windows.Forms.DataGridViewRowCancelEventHandler UserDeletingRow;
C#
public event System.Windows.Forms.DataGridViewRowCancelEventHandler? UserDeletingRow;
member this.UserDeletingRow : System.Windows.Forms.DataGridViewRowCancelEventHandler
Public Custom Event UserDeletingRow As DataGridViewRowCancelEventHandler
下面的代码示例演示如何使用 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;
}
}
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
可以取消此事件,以防止完成行删除。
有关如何处理事件的详细信息,请参阅 处理和引发事件。
产品 | 版本 |
---|---|
.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 |