This example demonstrates how to preserve the XtraGrid's expansion, selection, focused row and its position related to the top, and restore this state later.
For more information, please refer to the How to preserve the XtraGrid View state Knowledge Base article.
See Also:
How to preserve the expanded state of TreeList nodes when refreshing data
Question Comments
Added By: James Shipley at: 4/10/2014 6:29:37 AM
This worked great for my application where I needed to preserve grouping! thanks
Added By: Gilles Guerette at: 5/19/2014 9:00:19 AMThis is just what i needed. I've been fiddling with this for some time and this solves my problem. Thank you!
Added By: Harpreet Saini at: 2/19/2015 11:34:30 AMI had a problem with this, specifically with the SaveVisibleIndex and LoadVisibleIndex methods. I wrapped the call to set my XtraGrid's datasource property between calls to SaveViewInfo and LoadViewInfo.
Me.m_helper.SaveViewInfo()
Me.grdDailyAttendanceView.BeginUpdate()
Me.grdDailyAttendance.DataSource = assignmentsByDateDT
Me.grdDailyAttendanceView.EndUpdate()
Me.m_helper.LoadViewInfo()
When the datasource is changed from a datatable without any rows (empty) to a datatable with rows I got a Arithmetic Overload error. Expanding the SaveVisibleIndex and LoadVisibleIndex methods out:
Public Sub SaveVisibleIndex()
Dim fRHand As Integer = view.FocusedRowHandle
Dim vIdx As Integer = view.GetVisibleIndex(fRHand)
visibleRowIndex = vIdx - view.TopRowIndex
End Sub
Public Sub LoadVisibleIndex()
view.MakeRowVisible(view.FocusedRowHandle, True)
Dim fRHand As Integer = view.FocusedRowHandle
Dim vIdx As Integer = view.GetVisibleIndex(fRHand)
Dim myIndex As Integer = vIdx - visibleRowIndex
view.TopRowIndex = myIndex
End Sub
When the SaveVisibleIndex method is called when the grid's datasource is set to an empty datatable the FocusedRowHandle equals Integer.MinValue and the VisibleIndex is also Integer.MinValue. Concequently, the visibleRowIndex variable is set to Integer.MinValue.
When the LoadVisibleIndex method is called after the grid's datasource is set to a non-empty datatable, the FocusedRowHandle is equal to -1, and the value zero (0) is returned from the call to GetVisibleIndex when -1 is passed (in the expanded code above the variable vIdx = 0) . So, when the top row index is calculated => Dim myIndex As Integer = vIdx - visibleRowIndex it results in an arithmetic overload exception (0 - integer.minvalue). To avoid this, I inserted an if block in the LoadVisibleIndex method as follows:
Public Sub LoadVisibleIndex()
view.MakeRowVisible(view.FocusedRowHandle, True)
Dim fRHand As Integer = view.FocusedRowHandle
Dim vIdx As Integer = view.GetVisibleIndex(fRHand)
If visibleRowIndex = Integer.MinValue AndAlso vIdx <> Integer.MinValue Then
visibleRowIndex = 0
End If
Dim myIndex As Integer = vIdx - visibleRowIndex
view.TopRowIndex = myIndex
End Sub
Can someone from DevExpress please tell me if this solution is an optimal solution or if a different solution is recommended?
tyvm.
Hello Harpreet,
To process your recent post more efficiently, I created a separate ticket on your behalf: T211306: How to preserve the XtraGrid View state. This ticket is currently in our processing queue. Our team will address it as soon as we have any updates.