PC Review


Reply
Thread Tools Rate Thread

Re: Finding the new index of a selected row after Re-sort on a DataGrid.

 
 
One Handed Man
Guest
Posts: n/a
 
      26th Aug 2003
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...



 
Reply With Quote
 
 
 
 
Sam
Guest
Posts: n/a
 
      27th Aug 2003
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...

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataGrid Control Selected Index =?Utf-8?B?Q2hyaXM=?= Microsoft Dot NET Framework Forms 1 20th Jan 2005 09:10 PM
if I have a datagrid, how can i do the selectedindexchange on the selected index on that datagrid?? Jason Microsoft Dot NET Framework Forms 1 17th Oct 2003 02:46 PM
Retrieving data table index for datagrid index selected Dvae c Microsoft Dot NET Compact Framework 2 5th Oct 2003 02:43 PM
Need Help Determining DataGrid Column Selected for Sort using IBindingList - Datagrid.zip (0/1) David Elliott Microsoft Dot NET Framework Forms 1 18th Jul 2003 03:22 AM
Need Help Determining DataGrid Column Selected for Sort using IBindingList - Datagrid.zip (0/1) David Elliott Microsoft Dot NET Framework Forms 0 18th Jul 2003 03:12 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:26 PM.