We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Re-selecting current row after sorting GridDataBoundGrid

Hi, Forgive me if this has been asked (and answered) before. I need to highlight/re-select the previously selected row after sorting a GridDataBoundGrid. How do I do this? Thanks, Tim A.

2 Replies

AD Administrator Syncfusion Team November 30, 2004 08:44 PM UTC

You have to manage this yourself. You can do this by deriving the GridDataBoundGrid and overriding OnSortColumn. The code below assumes there is a PrimaryKey column in the database the the new row position can be easily found after the sort. Here is a forum link that shows another way of doing this. http://64.78.18.34/Support/Forums/message.aspx?MessageID=10655 Here is the derived grid code.
Public Class MyGridDataBoundGrid
	Inherits GridDataBoundGrid
	Public Overrides Sub SortColumn(ByVal colIndex As Integer)
		Dim cm As CurrencyManager = Me.Parent.BindingContext(Me.DataSource, Me.DataMember)
		Dim pdc As PropertyDescriptorCollection = cm.GetItemProperties
		Dim pd As PropertyDescriptor = pdc.Find("Col1", True) ’assumes Col1 is primary key column
		Dim val As Object = Me(Me.CurrentCell.RowIndex, 1).Text ’ assumes 1 is the col number of the primary key col, Col1.
		MyBase.SortColumn(colIndex)
		Dim i As Integer = CType(cm.List, IBindingList).Find(pd, val) + 1
		Me.CurrentCell.MoveTo(i, Me.CurrentCell.ColIndex, GridSetCurrentCellOptions.ScrollInView)
	End Sub
End Class


TA Tim Aquino December 1, 2004 04:35 PM UTC

Hi, The solution from the link works fine. The code below (derived GridDataBoundGrid) did not. Anyway, using the solution from the link you''ve provided, the assumption is we have a ''unique'' col (in the example, Col1). What if the datasource does not have a unique col? Is it still possible to re-select the previously selected row after a sort? Thanks, Tim A. >You have to manage this yourself. You can do this by deriving the GridDataBoundGrid and overriding OnSortColumn. The code below assumes there is a PrimaryKey column in the database the the new row position can be easily found after the sort. > >Here is a forum link that shows another way of doing this. >http://64.78.18.34/Support/Forums/message.aspx?MessageID=10655 > >Here is the derived grid code. > >
>Public Class MyGridDataBoundGrid
>	Inherits GridDataBoundGrid
>	Public Overrides Sub SortColumn(ByVal colIndex As Integer)
>		Dim cm As CurrencyManager = Me.Parent.BindingContext(Me.DataSource, Me.DataMember)
>		Dim pdc As PropertyDescriptorCollection = cm.GetItemProperties
>		Dim pd As PropertyDescriptor = pdc.Find("Col1", True) ’assumes Col1 is primary key column
>		Dim val As Object = Me(Me.CurrentCell.RowIndex, 1).Text ’ assumes 1 is the col number of the primary key col, Col1.
>		MyBase.SortColumn(colIndex)
>		Dim i As Integer = CType(cm.List, IBindingList).Find(pd, val) + 1
>		Me.CurrentCell.MoveTo(i, Me.CurrentCell.ColIndex, GridSetCurrentCellOptions.ScrollInView)
>	End Sub
>End Class
>

Loader.
Live Chat Icon For mobile
Up arrow icon