简体   繁体   English

在UserDeletingRow的dataTable中将行标记为已删除

[英]Mark row as deleted in dataTable on UserDeletingRow

I have a datatable bound to a winforms dataGridView via a BindingSourceControl. 我有一个通过BindingSourceControl绑定到winforms dataGridView的数据表。 I want to be able handle the UserDeletingRow event from the dataGridView and mark the row in my dataTable as deleted. 我希望能够从dataGridView处理UserDeletingRow事件,并将dataTable中的行标记为已删除。 I need to then be able to retrieve the rows marked as deleted from the datatable so that I can delete them from my database when a Save button is clicked. 然后,我需要能够从数据表中检索标记为已删除的行,以便在单击“保存”按钮时可以将其从数据库中删除。 Please not I dont want to delete from the database on each firing of UserDeletingRow, only mark that row as deleted in my dataset. 请不要在我每次UserDeletingRow触发时都不想从数据库中删除,只在我的数据集中将该行标记为已删除。

Can anyone point out how to do this? 谁能指出该怎么做?

The best I found to do this is be sure to call AcceptChanges() on the dataTable/dataset before editing starts - I did this on form load when the data was bound to the grid. 我发现要做的最好的事情是确保在编辑开始之前在dataTable / dataset上调用AcceptChanges()-我是在将数据绑定到网格时在窗体加载时执行此操作的。 Now when the user deletes records via the grid and then hits "save", I am able to filter the rows in the dataTable by the row's rowState to get the ones that have been deleted. 现在,当用户通过网格删除记录然后单击“保存”时,我能够按行的rowState过滤dataTable中的行,以获取已删除的行。 Note that the Count property on the rows collection of dataTable drops by one each time the user deletes a row, but the row is still in the dataTable, its just marked for deletion. 请注意,每当用户删除一行时,dataTable的行集合上的Count属性都会减少一,但是该行仍在dataTable中,它刚刚被标记为要删除。

在集合中捕获“待删除” rowId,然后创建一些方法并传递该集合以处理记录的删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM