0

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

我需要实现这种行为的单点联系是什么?

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

4

1 回答 1

0

You can handle the event UserDeletingRow to Cancel it manually and perform your own deletion like this:

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

You just said that your object has a property called IsDeleted, so I suppose it's called YourObject, you have to cast the DataBoundItem to that type so that you can access the IsDeleted property and set it to true. That's all.

于 2013-09-13T02:35:59.003 回答