DataGridView - select last row after reload

Hi I want to be able toselect the last row after theDataGridView refreshes. It auto
refills when a combobox is changed, and after the refill I want it to select the last row.
Anybody know how to do this? it is proving much harder than it sounds.

Details: The combobox is linked via a binding source to the DataGridView. This causes
the DataGridView to be filtered based on the ComboBox selection, and so changing the selection changes the DataGridView contents. See "Walkthrough: Creating a Lookup Table " http://msdn2.microsoft.com/en-us/library/ms171924.aspx
as an example of what I have done, although I used a DataGridView.

I have tried the 3 combobox events (SelectionChangeCommitted, SelectedIndexChanged, SelectedValueChanged)
and the DataGridView events ( RowsAdded, CellEnter ) but they do not work. The first 4 occur too late and
CellEnter throws a exception and does not seam to allow me to set the row after the refresh.
I have also tried setting BindingSource.Position as well as the DataGridView.CurrentCell, and both work fine, but I can't make them run after the automatic selection of the first row after the refresh completes.
Any ideas what event selects the first row of a DataGridView after a refresh, maybe I can use it?
Other solutions ?

Eddy.

[1486 byte] By [EddyJP] at [2007-12-24]
# 1
You may try bindingsource.ListChanged event
WangChi at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
Wang Chi,

Did not work, although works on first refresh (i.e. loading of the form)

// results in row 0 col 2 being selected
this.ordersDataGridView.CurrentCell = this.ordersDataGridView[2, 2];

// no impact, i.e. row 0 selected
this.ordersBindingSource.Position = 3;

// works fine but does not select anything, just shows the last row
this.ordersDataGridView.FirstDisplayedCell = this.ordersDataGridView[0,this.ordersDataGridView.Rows.Count - 1];

I used used ordersBindingSource.ListChanged (i.e. the DataGridView's binding source)

void ordersBindingSource_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e)

Is there some way to find out the order events are called and which events are called, maybe it would help pin down which event would be good to use.

Thanks for helping me with this.
Eddy.

EddyJP at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3

Try something like this:

Private Sub MyBindingSource_PositionChanged(...) Handles MyBindingSource.PositionChanged

If dgv.Rows.Count > 1 Then dgv.CurrentCell = dgv(0, dgv.Rows.Count - 2)

End Sub

Here is a sample project (vb).

vkh75 at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4
vkh75, yes it works, thanks. I added a few minor additions to stop cycling etc:

private bool selectLastPaymentRow = false;
private bool rowsAdded = false;

private void LookupControlWalkthrough_Load(object sender, EventArgs e)
{
.......
// after loading set to last position in DataViewGrid
this.ordersBindingSource.Position = this.ordersBindingSource.Count - 1;
}

void paymentsBindingSource_PositionChanged(object sender, System.EventArgs e)
{
// allows user to select other rows in grid
if (selectLastPaymentRow == true)
{
// prevent it from cycling and check it has more than one row
if (this.paymentsBindingSource.Count > 1 && this.paymentsBindingSource.Position != (this.paymentsBindingSource.Count - 1))
{
// select last row
this.paymentsBindingSource.Position = this.paymentsBindingSource.Count - 1;
// allows user to select other rows in grid
if (rowsAdded == true)
{
rowsAdded = false;
selectLastPaymentRow = false;
}
}
}
}

// Note sometimes paymentsBindingSource_PositionChanged is called
// before the rows are added to the grid, then again after they are added

void paymentsDataGridView_RowsAdded(object sender, System.Windows.Forms.DataGridViewRowsAddedEventArgs e)
{
if (selectLastPaymentRow == true)
{
rowsAdded = true;
this.paymentsBindingSource.Position = this.paymentsBindingSource.Count - 1;
}
}

void customerIDComboBox_SelectionChangeCommitted(object sender, System.EventArgs e)
{
selectLastPaymentRow = true;
rowsAdded = false;

}


EddyJP at 2007-8-31 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...