Ticket T904238
Visible to All Users

How to use the RefreshHelperClass in the latest version

created 5 years ago (modified 5 years ago)

[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

Show previous comments (3)

    Starting

    Visual Basic
    Try Dim helper As New RefreshHelper(devGridView, "Belegtyp") helper.SaveViewInfo() 'Fill Datasource clsGrid.FillGrid(devGridControl) helper.LoadViewInfo() Catch Return False End Try

    Refresh Datasource

    Visual Basic
    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

    Visual Basic
    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
    DevExpress Support Team 5 years ago

      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.

        1. It should save the expanded groups
        2. I refresh the Datasource with the same columns and perhaps getting new rows for user
        3. I wanna load the saved format
        Visual Basic
        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

        Answers

        created 5 years ago (modified 5 years ago)

        I´ve fixed the Problem.
        Sorry translated the iteration false.

        Visual Basic
        For i As Integer = -1 To Integer.MinValue + 1 Step -1

        Thank you for your Support, you are the best.

          Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

          Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.