0

I have a DataTable with many rows, and the DataTable is bound to a DataGrid. After deleting one or more rows via the DataGrid, I need to run specific, quite complex checks on the remaning rows.

I subscribe to the RowDeleted event, and call the checking method from there. 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. 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?

4

3 回答 3

2

你可以在计时器的帮助下做到这一点。声明一个全局定时器并设置它的属性:

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

现在在您的 RowDeleted-Event 中,您可以这样做:

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

因为计时器在 RowDeleted 事件中一遍又一遍地重新启动,所以您的更新逻辑只会在最后一个处理程序被触发后被调用。

于 2013-09-24T08:54:01.987 回答
0

我建议您将Checkbox列添加到您的 datagridview 并提供一个delete button并让用户仅通过选择每行上的相关复选框来删除单行或多行。

并在按钮的删除命令中添加检查剩余行的逻辑。这将确保您的逻辑将在每次删除时运行一次,而不管被删除的行数。

于 2013-09-24T08:39:36.800 回答
0

我会建议你处理Delete logic in your ViewModel类似的事情:

  1. 处理'Delete' keydown event and bind it to the Command on viewmodel使用交互性。
  2. SelectedItems将属性绑定DataGridCommandParameter删除命令。
  3. 在里面DeleteCommand handler, remove the items from the DataGrid's ItemsSource.
  4. 删除项目后,您可以运行检查
于 2013-09-24T08:43:15.140 回答