![](/img/trans.png)
[英]Winforms: Which event to fire? combobox.selectedindexchanged OR bindingsource.currentchanged
[英]Programmatically select a datagridview row doesn't fire bindingsource.currentchanged event
這是Winform C#問題。
我有一個綁定到綁定源的自定義datagridview。 有一個監聽器正在監聽bindingsource.currentchanged事件。
當我訂閱自定義的datagridview的sorted事件並以編程方式選擇一行時,不觸發bindingsource.currentchanged事件:
dataGridViewExtended.Sorted += SortedCompleted;
private void SortedCompleted(...){
// Some code to get rowIndex...
dataGridViewExtended.Rows[rowIndex].Selected = true;
}
為什么以編程方式更改datagridview行的選擇不會觸發bindingsource.currentchanged? 我該如何觸發該事件?
您可以使用CurrentCell屬性設置CurrentRow。
CurrentRow是只讀的。
Selected屬性不會影響CurrencyManager。
從某些DataGridView事件中的代碼更改CurrentRow存在一些限制,它可能引發異常。
要從Dgv事件更改CurrentRow,可以使用Control.BeginInvoke異步發布更改。
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
我找出原因了。 選擇DataGridView行不會觸發BindingSource的CurrentChanged事件。 部分原因是可以同時選擇多個行。 要觸發該事件,您需要設置DataGridView的CurrentCell。 任何時候,CurrentCell只能是一個。 一旦更新CurrentCell,就會觸發CurrentChanged事件。
就我而言,將所選行的第一個單元格設置為CurrentCell只是一件事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.