1
votes

This is a Winform C# question.

I have a customized datagridview which is bound to a bindingsource. There is a listener listening to bindingsource.currentchanged event.

When I subscribed the customized datagridview's sorted event and programmatically select a row, the bindingsource.currentchanged event is not fired:

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

Why programmatically change the selection of a datagridview row doesn't fire the bindingsource.currentchanged? How can I fire that event?

3

3 Answers

3
votes

You can use CurrentCell Property to set the CurrentRow.

CurrentRow is ReadOnly.

The Selected property do´nt affect to CurrencyManager.

There are some limitations to change the CurrentRow from code in some DataGridView Events, it can throw exception.

To change CurrentRow from a Dgv Event you can use Control.BeginInvoke to Async post the change.

2
votes

I found out the reason. Selecting DataGridView rows don't trigger CurrentChanged event of the BindingSource. Partly because there can be multiple rows selected at the same time. To trigger that event, you need to set CurrentCell of the DataGridView. Anytime, CurrentCell can only be one. As soon as you update the CurrentCell, the CurrentChanged event will be fired.

In my case, it is simply a matter to set the first cell of the selected row as the CurrentCell.

1
votes
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