2010-07-15 91 views
0

这是一个Winform C#问题。以编程方式选择一个datagridview行不会触发bindingsource.currentchanged事件

我有一个自定义的datagridview绑定到绑定源。有一个监听器正在监听bindingsource.currentchanged事件。

当我订阅的定制DataGridView中的排序活动,以编程方式选择一排,则不触发bindingsource.currentchanged事件:

dataGridViewExtended.Sorted += SortedCompleted; 
private void SortedCompleted(...){ 
    // Some code to get rowIndex... 
    dataGridViewExtended.Rows[rowIndex].Selected = true;  
} 

为什么编程方式更改一个DataGridView行的选择不火BindingSource的。 currentchanged?我怎么能解雇那个事件?

回答

3

您可以使用CurrentCell Property来设置CurrentRow。

CurrentRow是ReadOnly。

精选属性不会影响到CurrencyManager。

从某些DataGridView事件中的代码中更改CurrentRow有一些限制,它可能会引发异常。

要从Dgv事件中更改CurrentRow,您可以使用Control.BeginInvoke来异步发布更改。

+0

表单继承自Control。编写BeginInvoke,你可以调用YourForm.BeginInvoke。 – x77 2010-07-15 06:18:48

1

我找出原因。选择DataGridView行不会触发BindingSource的CurrentChanged事件。部分原因是可能同时选择多行。要触发该事件,您需要设置DataGridView的CurrentCell。任何时候,CurrentCell只能是一个。只要更新CurrentCell,CurrentChanged事件就会被触发。

就我而言,将所选行的第一个单元格设置为CurrentCell是一件简单的事情。

1
CurrencyManager DtCm; //SETUP CURRENCY MANAGER 
DtCm = (CurrencyManager)this.BindingContext[DtTable]; //BIND CURRENCY MANAGER 
int F = Dgv.CurrentRow.Index; //SET INDEX POSITION 
this.DtCm.Position = F; //RESTORE POSITION 
相关问题