如何在数据网格视图中实现软删除

本文关键字:实现 删除 视图 网格 数据 数据网 | 更新日期: 2023-09-27 18:30:59

我有一个绑定到对象数据源的 DataGridView。该对象具有属性"IsDeleted"。当用户按下删除键,或单击删除按钮,或以其他方式删除行时,我想设置"IsDeleted"标志而不是删除行。(然后我希望数据网格视图更新)。

实现此行为所需的单点联系是什么?

我不想尝试单独处理所有用户路径。

如何在数据网格视图中实现软删除

您可以处理事件UserDeletingRow手动Cancel它并执行您自己的deletion,如下所示:

private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e){
   e.Cancel = true;//Cancel the actual deletion of row
   //You can just hide the row instead
   e.Row.Visible = false;
   //Then set the IsDeleted of the underlying data bound item to true
   ((YourObject)e.Row.DataBoundItem).IsDeleted = true;
}

你刚才说你的对象有一个名为IsDeleted的属性,所以我想它叫YourObject,你必须将DataBoundItem强制转换为该类型,以便您可以访问IsDeleted属性并将其设置为 true 。就这样。