0
votes

I have a DataGridView in WinForms. I am programmatically setting the selected row using:

int index = CompoundListSource.Find("ID", previousAzeotrope.Compound1.ID);
CompoundListSource.Position = index;

For context:

BindingSource CompoundListSource = new BindingSource();
CompoundListSource.DataSource = [A DataTable];

The selection works fine. However, my DataGridView is rather large, so I have it calculate the row heights by listening to its Scroll event. When the DataGridView is scrolled, I call:

GridView.AutoResizeRows(DataGridViewAutoSizeRowsMode.DisplayedCellsExceptHeaders)

As a result of this resizing, the selected row is often no longer among the displayed rows.

It's not a serious problem, but it's a nuisance. Does anyone here know how to solve this problem?

1

1 Answers

2
votes

You can set the DataGridView's CurrentCell to ensure a particular cell is in view:

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx

You can also set:

DataGridView.FirstDisplayedScrollingRowIndex = <row index>;

However, this will probably not work in a scroll handler as it actually raises a scroll event itself! I'd give them both a try.