[DevExpress Support Team: CLONED FROM A2161: How to preserve the XtraGrid View state]
Hello,
is it working at the new version ?
https://github.com/DevExpress-Examples/how-to-preserve-the-xtragrid-view-state-e776/blob/13.1.4%2B/CS/RefreshHelperClass.cs
I´ve tried it with this solution, but the Arraylist always stay empty
Hello,
The latest version should also work correctly. If you see different behavior, please clarify when you save and restore the GridControl's state. It would help if you modify our example to demonstrate your scenario.
I look forward to your response.
I have an GridView called devGridView
Inserted the RefreshHelper Class and call it like
Dim helper As New RefreshHelper(devGridView, "ID")
helper.SaveViewInfo()
Here i will Refresh the GridView like
GridControl.DataSource = myDatatable
helper.LoadViewInfo()
Always cannot load the stored with the method LoadViewInfo because the ArrayList´s aren´t filled.
The data inside the devGridView isn´t setted up with foreign or primary Key,
only assigned as in my example.
Thank you for the clarification. You can set breakpoints in the SaveSelectionViewInfo, SaveExpansionViewInfo and SaveExpandedMasterRows methods to determine why items are not added in array lists.
If it does not help, please share a project to illustrate this behavior so we can analyze it on our side.
Starting
Try Dim helper As New RefreshHelper(devGridView, "Belegtyp") helper.SaveViewInfo() 'Fill Datasource clsGrid.FillGrid(devGridControl) helper.LoadViewInfo() Catch Return False End Try
Refresh Datasource
Friend Shared Sub FillGrid(ByRef GridControl As DevExpress.XtraGrid.GridControl) Dim DBConnection As New DBSQLQuery Dim dt As New DataTable 'Query Dim sqlString As String = "Connection String" dt = DBConnection.SelectMoreDB(sqlString, DBConnection.conAuswertung) 'Set GridControl.DataSource = dt End Sub
RefreshHelper
Imports DevExpress.XtraGrid Imports DevExpress.XtraGrid.Columns Imports DevExpress.XtraGrid.Views.Grid Public Class RefreshHelper <Serializable> Public Structure RowInfo Public Id As Object Public level As Integer End Structure Private view As GridView Private keyFieldName As String Private _saveExpList As ArrayList Private _saveSelList As ArrayList Private _saveMasterRowsList As ArrayList Private visibleRowIndex As Integer = -1 Public Sub New(ByVal view As GridView, ByVal keyFieldName As String) Me.view = view Me.keyFieldName = keyFieldName End Sub Public ReadOnly Property SaveExpList As ArrayList Get If _saveExpList Is Nothing Then _saveExpList = New ArrayList() End If Return _saveExpList End Get End Property Public ReadOnly Property SaveSelList As ArrayList Get If _saveSelList Is Nothing Then _saveSelList = New ArrayList() End If Return _saveSelList End Get End Property Public ReadOnly Property SaveMasterRowsList As ArrayList Get If _saveMasterRowsList Is Nothing Then _saveMasterRowsList = New ArrayList() End If Return _saveMasterRowsList End Get End Property Protected Function FindParentRowHandle(ByVal rowInfo As RowInfo, ByVal rowHandle As Integer) As Integer Dim result As Integer = view.GetParentRowHandle(rowHandle) While view.GetRowLevel(result) <> rowInfo.level result = view.GetParentRowHandle(result) End While Return result End Function Protected Sub ExpandRowByRowInfo(ByVal rowInfo As RowInfo) Dim dataRowHandle As Integer = view.LocateByValue(0, view.Columns(keyFieldName), rowInfo.Id) If dataRowHandle <> GridControl.InvalidRowHandle Then Dim parentRowHandle As Integer = FindParentRowHandle(rowInfo, dataRowHandle) view.SetRowExpanded(parentRowHandle, True, False) End If End Sub Protected Function GetRowHandleToSelect(ByVal rowInfo As RowInfo) As Integer Dim dataRowHandle As Integer = view.LocateByValue(0, view.Columns(keyFieldName), rowInfo.Id) If dataRowHandle <> GridControl.InvalidRowHandle Then If view.GetRowLevel(dataRowHandle) <> rowInfo.level Then Return FindParentRowHandle(rowInfo, dataRowHandle) End If Return dataRowHandle End Function Protected Sub SelectRowByRowInfo(ByVal rowInfo As RowInfo, ByVal isFocused As Boolean) If isFocused Then view.FocusedRowHandle = GetRowHandleToSelect(rowInfo) Else view.SelectRow(GetRowHandleToSelect(rowInfo)) End If End Sub Public Sub SaveSelectionViewInfo(ByVal list As ArrayList) list.Clear() Dim column As GridColumn = view.Columns(keyFieldName) Dim rowInfo As RowInfo Dim selectionArray As Integer() = view.GetSelectedRows() If selectionArray IsNot Nothing Then For i As Integer = 0 To selectionArray.Length - 1 Dim dataRowHandle As Integer = selectionArray(i) rowInfo.level = view.GetRowLevel(dataRowHandle) If dataRowHandle < 0 Then dataRowHandle = view.GetDataRowHandleByGroupRowHandle(dataRowHandle) rowInfo.Id = view.GetRowCellValue(dataRowHandle, column) list.Add(rowInfo) Next End If rowInfo.Id = view.GetRowCellValue(view.FocusedRowHandle, column) rowInfo.level = view.GetRowLevel(view.FocusedRowHandle) list.Add(rowInfo) End Sub Public Sub SaveExpansionViewInfo(ByVal list As ArrayList) If view.GroupedColumns.Count = 0 Then Return list.Clear() Dim column As GridColumn = view.Columns(keyFieldName) For i As Integer = -1 To Integer.MinValue + 1 If Not view.IsValidRowHandle(i) Then Exit For If view.GetRowExpanded(i) Then Dim rowInfo As RowInfo Dim dataRowHandle As Integer = view.GetDataRowHandleByGroupRowHandle(i) rowInfo.Id = view.GetRowCellValue(dataRowHandle, column) rowInfo.level = view.GetRowLevel(i) list.Add(rowInfo) End If Next End Sub Public Sub SaveExpandedMasterRows(ByVal list As ArrayList) If view.GridControl.Views.Count = 1 Then Return list.Clear() Dim column As GridColumn = view.Columns(keyFieldName) For i As Integer = 0 To view.DataRowCount - 1 If view.GetMasterRowExpanded(i) Then list.Add(view.GetRowCellValue(i, column)) Next End Sub Public Sub SaveVisibleIndex() visibleRowIndex = view.GetVisibleIndex(view.FocusedRowHandle) - view.TopRowIndex End Sub Public Sub LoadVisibleIndex() view.MakeRowVisible(view.FocusedRowHandle, True) view.TopRowIndex = view.GetVisibleIndex(view.FocusedRowHandle) - visibleRowIndex End Sub Public Sub LoadSelectionViewInfo(ByVal list As ArrayList) view.BeginSelection() Try view.ClearSelection() For i As Integer = 0 To list.Count - 1 SelectRowByRowInfo(CType(list(i), RowInfo), i = list.Count - 1) Next Finally view.EndSelection() End Try End Sub Public Sub LoadExpansionViewInfo(ByVal list As ArrayList) If view.GroupedColumns.Count = 0 Then Return view.BeginUpdate() Try view.CollapseAllGroups() For Each info As RowInfo In list ExpandRowByRowInfo(info) Next Finally view.EndUpdate() End Try End Sub Public Sub LoadExpandedMasterRows(ByVal list As ArrayList) view.BeginUpdate() Try view.CollapseAllDetails() Dim column As GridColumn = view.Columns(keyFieldName) For i As Integer = 0 To list.Count - 1 Dim rowHandle As Integer = view.LocateByValue(0, column, list(i)) view.SetMasterRowExpanded(rowHandle, True) Next Finally view.EndUpdate() End Try End Sub Public Sub SaveViewInfo() SaveExpandedMasterRows(SaveMasterRowsList) SaveExpansionViewInfo(SaveExpList) SaveSelectionViewInfo(SaveSelList) SaveVisibleIndex() End Sub Public Sub LoadViewInfo() LoadExpandedMasterRows(SaveMasterRowsList) LoadExpansionViewInfo(SaveExpList) LoadSelectionViewInfo(SaveSelList) LoadVisibleIndex() End Sub End Class
Hi,
Do I understand you correctly that the grid is already bound to another data source when you call the SaveViewInfo method? If so, what information do you wish to save (selection, expanded group rows or master rows)?
The approach suggested by Nadezhda is the most efficient to find the cause of the issue. Insert a break point into the SaveViewInfo method and go line by line to see why the required information is not saved. Refer to Debugging for absolute beginners and Learn to debug Visual Basic.
If that is not helpful, we need a small compilable sample illustrating the issue for investigation.
I know how i need to Debug.
I only need the expanding.
Public Sub SaveExpansionViewInfo(ByVal list As ArrayList) If view.GroupedColumns.Count = 0 Then Return list.Clear() Dim column As GridColumn = view.Columns(keyFieldName) For i As Integer = -1 To Integer.MinValue + 1 'The Problem is here 'keyFieldName is setted to column If Not view.IsValidRowHandle(i) Then Exit For If view.GetRowExpanded(i) Then Dim rowInfo As RowInfo Dim dataRowHandle As Integer = view.GetDataRowHandleByGroupRowHandle(i) rowInfo.Id = view.GetRowCellValue(dataRowHandle, column) rowInfo.level = view.GetRowLevel(i) list.Add(rowInfo) End If Next End Sub