简体   繁体   English

删除多个DataTable行后仅触发一个事件

[英]Fire only one event after deleting multiple DataTable rows

I have a DataTable with many rows, and the DataTable is bound to a DataGrid . 我有一个DataTable有许多行,而DataTable绑定到DataGrid After deleting one or more rows via the DataGrid , I need to run specific, quite complex checks on the remaning rows. 通过DataGrid删除一行或多行后,我需要对重新行进行特定的,非常复杂的检查。

I subscribe to the RowDeleted event, and call the checking method from there. 我订阅了RowDeleted事件,并从那里调用了检查方法。 My problem is, when deleting multiple rows via the DataGrid , the RowDeleted event is fired for every single row, calling the checking method every time. 我的问题是,当通过DataGrid删除多行时,会为每一行触发RowDeleted事件,每次调用check方法。 As the checking method checks all the remaining rows and all their columns, it is a big slowdown for my application, also very redundant. 由于检查方法检查所有剩余的行及其所有列,这对我的应用程序来说是一个很大的减速,也非常多余。 It would be enough to run it once all the selected rows are deleted. 删除所有选定的行后,运行它就足够了。

Is there a way to fire only one event after deleting any number of rows? 删除任意数量的行后,有没有办法触发一个事件?

You could do this with the help of a timer. 你可以借助计时器来做到这一点。 Declare a global timer and set its properties: 声明全局计时器并设置其属性:

System.Timers.Timer _delayTimer = new System.Timers.Timer(1000);
_delayTimer.Elapsed += new EventHandler(_delayTimer_Elapsed);

 void _delayTimer_Elapsed(object sender, EventArgs e)
 {
     _delayTimer.Stop();
     Dispatcher.Invoke(new Action(UpdateMethodName)); 
     //or - with passing arguments:
     Dispatcher.Invoke(new Action<string>(UpdateMethodName), new object[]{"argument"});
 }

Now in your RowDeleted-Event, you do this: 现在在你的RowDeleted-Event中,你这样做:

_delayTimer.Stop();
_delayTimer.Start();

Because the timer gets restarted in the RowDeleted-event over and over again, your update logic will only get called after the last handler is fired. 因为计时器一次又一次地在RowDeleted事件中重新启动,所以只有在最后一个处理程序被触发后才会调用更新逻辑。

I would suggest you to add Checkbox column to your datagridview and provide a delete button and let the users to delete single or multiple rows only by selecting relevant checkbox on each row. 我建议你将Checkbox列添加到datagridview并提供一个delete button ,让用户只能通过选中每行的相关复选框来删除单行或多行。

And add your logic which checkes remaing rows within delete command of the button. 并添加您的逻辑,检查按钮的删除命令中的重新存储行。 This will make sure that your logic will run once per delete regardless numbers of rows being deleted. 这将确保您的逻辑将在每次删除时运行一次,而不管删除的行数是多少。

I will suggest you to handle the Delete logic in your ViewModel like: 我建议你Delete logic in your ViewModel处理Delete logic in your ViewModel如:

  1. Handle the 'Delete' keydown event and bind it to the Command on viewmodel using interactivity. 处理'Delete' keydown event and bind it to the Command on viewmodel使用交互性'Delete' keydown event and bind it to the Command on viewmodel
  2. Bind the SelectedItems property of DataGrid to the CommandParameter for the Delete Command. DataGridSelectedItems属性绑定到Delete Command的CommandParameter
  3. In the DeleteCommand handler, remove the items from the DataGrid's ItemsSource. DeleteCommand handler, remove the items from the DataGrid's ItemsSource.
  4. After deleting the items, you can run your checks 删除项目后,您可以运行检查

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

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