I sort of figured out, but you have to use a loop. Someone answered
my other post (
http://groups.google.com/groups?hl=e...com%26rnum%3D2).
What I did is set up two form variable
Private m_blnSelectOnPaint As Boolean = FALSE
Private m_intCode as Integer
(m_intCode - the primary key - should be stored each time user selects
a row)
Then, in the dataview's ListChanged event, If there's been a change in
the List (resort) I set this variable to TRUE, so then in the grids
paint event, I know to search for the row. I also store
Private Sub m_dvwBooksView_ListChanged(ByVal sender As Object, _
ByVal e As System.ComponentModel.ListChangedEventArgs) _
Handles m_dvwBooksView.ListChanged
m_blnSelectOnPaint = True
End Sub
Then the Paint event looks like this.
'---------------------------------------------------------------------------
'PURPOSE: If the DataGrid is being Painted due to a re-sort,
' then find the new index of the previously hi-lited
' row, and hi-lited it again.
'----------------------------------------------------------------------------
Private Sub grdBooks_Paint(ByVal sender As Object, _
ByVal e As System.Windows.Forms.PaintEventArgs) Handles
grdBooks.Paint
Dim intIndex As Integer
Dim drwSearchRow As DataRow
Dim blnFound As Boolean = False
With m_dstBooks.Tables("Books")
'If we need to reselect the hi-lited row,
'then search for it's new index.
If (m_blnSelectOnPaint) Then
m_blnSelectOnPaint = False
'Obtain the DataRow of the Book we need to find.
'In order to do this the primary key needs to be set in
'the DataTable (
drwSearchRow = .Rows.Find(m_intCode)
'Search for the DataRow in the newly
'sorted DataGrid/ListView
For intIndex = 0 To .Rows.Count - 1
If (drwSearchRow Is _
m_dvwBooksView.Item(intIndex).Row) Then
blnFound = True
Exit For
End If
Next
'If the DataRow was found, hi-lite it again.
If (blnFound) Then
grdBooks.CurrentRowIndex = intIndex
SelectRow(intIndex)
End If
End If
End With
End Sub
Private Sub SelectRow(ByVal intRow As Integer)
grdBooks.Select(intRow)
m_intCode = GetRowsCode()
End Sub
Private Function GetRowsCode() As Integer
Dim bmbManager As BindingManagerBase = _
grdBooks.BindingContext(grdBooks.DataSource, _
grdBooks.DataMember)
Return CType(bmbManager.Current, _
DataRowView).Row.Item("iCode")
End Function
To set the primary key in the DataTable:
m_dstBooks.Tables("Books").PrimaryKey = New DataColumn() _
{m_dstBooks.Tables("Books").Columns("iCode")}
This works for me pretty well, but I can see it possibly not working
great with large datasets.
"One Handed Man" <terry_burnsREMOVE%FOR%NO%(E-Mail Removed)> wrote in message news:<uDBfAl$(E-Mail Removed)>...
> My sympathies go to you on this one as I am struggling with exactly the same
> problem. One would expect a dataView to maintain an pre-modified index so
> you could allways determine the position of the underlying data. It doesent
> seem to be available.( if it is I missed it !! )
>
> Unless someone else knows how to do this, I suggest you wrap the class and
> overload the functions which you need and code in for that functionality.
>
> --
> Regards - One Handed Man
>
> Author : Fish .NET & Keep .NET
>
> ==============================
> "Sam" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > I posted this on microsoft.public.dotnet.framework and haven't gotten
> > a response, so I'll try posting it here.
> >
> > 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?
> >
> > So if row B is selected with index 1 below:
> > A
> > B
> > C
> >
> > After the re-sort, I need to find the new index of B (2):
> > A
> > C
> > B
> >
> >
> > Thanks...