tags:

views:

24

answers:

2

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?

A: 

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.

Steve
A: 

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.

x77
Form inherits from Control. Writing BeginInvoke, you call YourForm.BeginInvoke.
x77