0

好吧,我有以下问题:

我需要在不触发 CurrentChanged 事件的情况下从绑定源中删除一行。删除工作没有任何问题。但它会立即引发 CurrentChanged 事件,这导致我正在执行与 CurrentChanged 事件匹配的代码。这导致了一个问题。.Delete()有什么方法可以在不引发事件的情况下达到类似的效果?

4

1 回答 1

0

如果有任何订阅者,删除一行将始终引发事件。

如果事件代码在您的控制之下,您可以设置一个您在 BindingSource_CurrentChanged 事件处理程序中检查的标志:

private void DeleteRow()
{
  this.justDeletedRow = true;
  this.bindingSource.DeleteRow(...);
}

protected void BindingSource_CurrentChanged(object sender ...)
{
  if (this.justDeletedRow)
  { 
     this.justDeletedRow = false;
     return;
  }

  // Process changes otherwise..
}

如果代码不在您的控制之下 - 例如,如果您绑定到组件 - 那么您可以在执行操作时取消绑定处理程序:

private void DeleteRow()
{
  this.bindingSource.CurrentChanged -= this.component.BindingSource_CurrentChanged;
  this.bindingSource.DeleteRow(...);
  this.bindingSource.CurrentChanged += this.component.BindingSource_CurrentChanged;
}
于 2012-05-15T21:29:55.550 回答