Datagrid - keep selected row after user re-sorts

S

Sam

Ok.

I have a dataset with a datatable called "Books".

Books contains ISBN, TITLE, and AUTHOR

In my datagrid, I only show TITLE and AUTHOR

I also have a dataview set up with it:

cm = CType(Me, Control).BindingContext(grdBooks.DataSource, _
grdBooks.DataMember)
m_dvwBookView = CType(cm.List, DataView)

I want to be able to keep the row selected, even after the user
re-sorts the datagrid by clicking on a column heading. I can store
the selected ISBN number when the user selects a new row, but I can't
figure out how to find the row with that ISBN #, after the user has
sorted by say TITLE. I can get the event when a user sorts by title
or author with the DataViews ListChanged event:

Private Sub m_dvwBookView_ListChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ListChangedEventArgs) _
Handles m_dvwBookView.ListChanged

If (e.ListChangedType = ComponentModel.ListChangedType.Reset) Then
grdBooks.Select(OldRowSelected)
End If
End Sub

I can't figure out how to get the new row index of the previously
selected row. Any ideas?


Thanks...
 
E

Eric Cadwell

I prefer to use DataTable.Rows.Find. (You can get the Table via the
DataView.Table property)
((DataTable)Grid.DataSource).Rows.Find(PrimaryKey);

Or you can loop the rows and check the value.

Use DataGrid.Select(int row) when you get the index of the row.

HTH,
Eric Cadwell
http://www.origincontrols.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top