New to Telerik UI for ASP.NET AJAX? Download free 30-day trial

Saving Grid Settings on a Per User Basis

This approach is obsoleted. In case you need to persist the grid's setting we strongly recommend to use RadPersistenceManager control.

Following is a small application that demonstrates a way to persist runtime settings of Telerik RadGrid in a variety of formats. You can persist the extracted settings on any medium - session, application variables, database, etc. For your convenience, the functionality is isolated in a single class called GridSettingsPersister. It is designed to save from and load into Telerik RadGrid the following runtime settings:

  • Page size

  • Group-by expressions

  • Sort expressions

  • Filter expression

  • Column settings

  • Width

  • OrderIndex

  • Display

  • Visible

  • CurrentFilterFunction

  • CurrentFilterValue

How to save grid settings

To use the persister, create a new instance of the GridSettingsPersister class, passing the RadGrid instance whose settings will be imported/exported in the constructor. To extract and serialize the current grid settings into a string, call the SaveSettings method of the object. Alternatively, you can use the GetSavedSettings method to retrieve the extracted settings as an instance of GridSettingsCollection. Instances of this class can serialize themselves to either a string or a byte array using the ToString and ToArray methods, respectively. Thus, you have the flexibility to save the extracted settings in one of the 3 formats:

  • string - settings are serialized to a string, much like the ViewState mechanism

  • byte[] - settings are serialized to a byte array

  • GridSettingsCollection instance - settings are not serialized and can be saved as an object

To save RadGrid's settings, call the SaveSettings method of the persister after making sure no further changes to RadGrid will be made. In a regular page life cycle, use the Page_PreRender method of the Page, or override and use the Render method for this purpose.

How to load grid settings

To load settings into the RadGrid instance, call the LoadSettings method of the GridSettingsPersister instance, passing the settings in one of the 3 accepted types:

  • string - settings serialized to a string

  • byte[] - settings serialized to a byte array

  • GridSettingsCollection instance - original non-serialized settings container

To load settings on initial page load, use the Init or Load events of the page to call LoadSettings. If RadGrid is databound in any of these, make sure you load the settings before binding the grid. Alternatively, if you are loading grid settings in a postback event of another control, call RadGrid.Rebind() to apply the restored settings.

See it in action

To see the GridSettingsPersister in action, please visit the Persisting Grid Settings online demo.

Show me the code

Following is the definition of the GridSettingsPersister class along with a couple of other helper classes that implement setting persistence for RadGrid:

/// <summary>
/// Imports and exports settings from a RadGrid instance.
/// </summary>
public class GridSettingsPersister
{
    public GridSettingsPersister()
        : this(null)
    {
    }
    /// <summary>
    /// Initializes an instance of GridSettingsPersister from a RadGrid instance
    /// </summary>
    /// <param name="grid">The RadGrid instance to import and exports settings</param>
    public GridSettingsPersister(RadGrid grid)
        : this(grid, GridSettingsType.All)
    {
    }
    /// <summary>
    /// Initializes an instance of GridSettingsPersister from a RadGrid instance
    /// and a collection GridSettingsType values
    /// </summary>
    /// <param name="grid">The RadGrid instance to import and exports settings</param>
    /// <param name="persistedSettings">
    /// A collection of GridSettingType values specifying the type of grid settings
    /// to import or export
    /// </param>
    public GridSettingsPersister(RadGrid grid, GridSettingsType persistedSettingFlags)
    {
        _grid = grid;
        _persistedSettingTypes = persistedSettingFlags;
        _settings = new GridSettingsCollection();
        _settings.ColumnSettings = new List<ColumnSettings>();
        _settings.AutoGeneratedColumnSettings = new List<ColumnSettings>();
    }
    private RadGrid _grid;
    private GridSettingsType _persistedSettingTypes;
    private GridSettingsCollection _settings;
    /// <summary>
    /// The underlyiong RadGrid instance to import or export settings from
    /// </summary>
    public RadGrid Grid
    {
        get
        {
            return _grid;
        }
        set
        {
            _grid = value;
        }
    }
    /// <summary>
    /// Gets or sets the GridSettingType flags that specify the grid settings to
    /// export or import
    /// </summary>
    public virtual GridSettingsType PersistedSettingTypes
    {
        get
        {
            return _persistedSettingTypes;
        }
        set
        {
            _persistedSettingTypes = value;
        }
    }
    protected virtual GridSettingsCollection Settings
    {
        get
        {
            return _settings;
        }
        set
        {
            _settings = value;
        }
    }
    /// <summary>
    /// Saves the current grid settings and returns the settings serilized to string
    /// </summary>
    public virtual string SaveSettings()
    {
        return GetSavedSettings().ToString();
    }
    /// <summary>
    /// Saves the current grid settings and retrieves the underlying
    /// GridSettingsCollection instance that contains the grid settings
    /// </summary>
    public virtual GridSettingsCollection GetSavedSettings()
    {
        if (Grid == null)
        {
            throw new NullReferenceException();
        }
        if (IsSettingSpecified(GridSettingsType.Paging))
            SavePagingSettings();
        if (IsSettingSpecified(GridSettingsType.Grouping))
            SaveGroupByExpressions();
        if (IsSettingSpecified(GridSettingsType.Sorting))
            SaveSortExpressions();
        if (IsSettingSpecified(GridSettingsType.Filtering))
            SaveFilterExpression();
        if (IsSettingSpecified(GridSettingsType.ColumnSettings))
            SaveColumnSettings();
        return Settings;
    }
    protected bool IsSettingSpecified(GridSettingsType settingType)
    {
        return (PersistedSettingTypes & GridSettingsType.All) == GridSettingsType.All ||
               (PersistedSettingTypes & settingType) == settingType;
    }
    protected virtual void SavePagingSettings()
    {
        Settings.PageSize = Grid.PageSize;
    }
    protected virtual void SaveGroupByExpressions()
    {
        Settings.GroupByExpressionsStates = new object[Grid.MasterTableView.GroupByExpressions.Count];
        for (int i = 0; i < Settings.GroupByExpressionsStates.Length; i++)
        {
            Settings.GroupByExpressionsStates[i] = ((IStateManager)Grid.MasterTableView.GroupByExpressions[i]).SaveViewState();
        }
    }
    protected virtual void SaveSortExpressions()
    {
        Settings.SortExpressionsState = ((IStateManager)Grid.MasterTableView.SortExpressions).SaveViewState();
    }
    protected virtual void SaveFilterExpression()
    {
        Settings.FilterExpression = Grid.MasterTableView.FilterExpression;
    }
    protected virtual void SaveColumnSettings()
    {
        Settings.ColumnSettings.Clear();
        foreach (GridColumn column in Grid.MasterTableView.Columns)
        {
            Settings.ColumnSettings.Add(GetColumnSettings(column));
        }
        Settings.AutoGeneratedColumnSettings.Clear();
        foreach (GridColumn column in Grid.MasterTableView.AutoGeneratedColumns)
        {
            Settings.AutoGeneratedColumnSettings.Add(GetColumnSettings(column));
        }
    }
    private ColumnSettings GetColumnSettings(GridColumn column)
    {
        ColumnSettings colSettings = new ColumnSettings();
        colSettings.UniqueName = column.UniqueName;
        colSettings.Width = column.HeaderStyle.Width;
        colSettings.Visible = column.Visible;
        colSettings.Display = column.Display;
        colSettings.OrderIndex = column.OrderIndex;
        colSettings.CurrentFilterFunction = column.CurrentFilterFunction;
        colSettings.CurrentFilterValue = column.CurrentFilterValue;
        return colSettings;
    }
    private void SetColumnSettings(ref GridColumn column, ColumnSettings setting)
    {
        column.Display = setting.Display;
        column.Visible = setting.Visible;
        column.HeaderStyle.Width = setting.Width;
        column.OrderIndex = setting.OrderIndex;
        column.CurrentFilterFunction = setting.CurrentFilterFunction;
        column.CurrentFilterValue = setting.CurrentFilterValue;
    }
    /// <summary>
    /// Loads grids settings from a serialized string
    /// </summary>
    /// <param name="value">The string that contains the serialized settings</param>
    public virtual void LoadSettings(string value)
    {
        LoadSettings(GridSettingsCollection.LoadFromSerializedData(value));
    }
    /// <summary>
    /// Loads grids settings from a byte array
    /// </summary>
    /// <param name="data">The byte array that contains the serialized grid settings</param>
    public virtual void LoadSettings(byte[] data)
    {
        LoadSettings(GridSettingsCollection.LoadFromSerializedData(data));
    }
    /// <summary>
    /// Loads grid settings from a GridSettingsCollection instance
    /// </summary>
    /// <param name="settings">The GridSettingsCollection instance to load settings from</param>
    public virtual void LoadSettings(GridSettingsCollection settings)
    {
        if (Grid == null || settings == null)
        {
            throw new NullReferenceException();
        }
        Settings = settings;
        if (IsSettingSpecified(GridSettingsType.Paging))
            LoadPagingSettings();
        if (IsSettingSpecified(GridSettingsType.Grouping))
            LoadGroupByExpressions();
        if (IsSettingSpecified(GridSettingsType.Sorting))
            LoadSortExpressions();
        if (IsSettingSpecified(GridSettingsType.Filtering))
            LoadFilterExpression();
        if (IsSettingSpecified(GridSettingsType.ColumnSettings))
            LoadColumnSettings();
    }
    protected virtual void LoadPagingSettings()
    {
        if (Grid.AllowPaging && Settings.PageSize > 0)
        {
            Grid.PageSize = Settings.PageSize;
        }
    }
    protected virtual void LoadGroupByExpressions()
    {
        if (Settings.GroupByExpressionsStates == null)
            return;
        Grid.MasterTableView.GroupByExpressions.Clear();
        foreach (object expressionState in Settings.GroupByExpressionsStates)
        {
            GridGroupByExpression expression = new GridGroupByExpression();
            ((IStateManager)expression).LoadViewState(expressionState);
            Grid.MasterTableView.GroupByExpressions.Add(expression);
        }
    }
    protected virtual void LoadSortExpressions()
    {
        if (Settings.SortExpressionsState == null)
            return;
        ((IStateManager)Grid.MasterTableView.SortExpressions).LoadViewState(Settings.SortExpressionsState);
    }
    protected virtual void LoadFilterExpression()
    {
        Grid.MasterTableView.FilterExpression = Settings.FilterExpression;
    }
    protected virtual void LoadColumnSettings()
    {
        if (Settings.AutoGeneratedColumnSettings.Count > 0)
        {
            Grid.ColumnCreated += new GridColumnCreatedEventHandler(Grid_ColumnCreated);
        }
        foreach (ColumnSettings colSetting in Settings.ColumnSettings)
        {
            GridColumn column = Grid.MasterTableView.GetColumnSafe(colSetting.UniqueName);
            if (column != null)
            {
                SetColumnSettings(ref column, colSetting);
            }
        }
    }
    private void Grid_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
        ColumnSettings settings = Settings.AutoGeneratedColumnSettings.Find(delegate(ColumnSettings set)
        {
            return set.UniqueName == e.Column.UniqueName;
        });
        GridColumn column = e.Column;
        if (settings != null)
        {
            SetColumnSettings(ref column, settings);
        }
    }
}
/// <summary>
/// Enumerates the types of grid settings that can be persisted
/// </summary>
[Flags]
public enum GridSettingsType
{
    Paging = 1,
    Sorting = 2,
    Filtering = 4,
    Grouping = 8,
    ColumnSettings = 16,
    All = 32
}
/// <summary>
/// Represents a collection of grid settings
/// </summary>
[Serializable]
public class GridSettingsCollection
{
    private int _pageSize;
    private object[] _groupByExpressionsStates;
    private object _sortExpressionsState;
    private string _filterExpression;
    private List<ColumnSettings> _columnSettings;
    private List<ColumnSettings> _autoColumnSettings;
    public int PageSize
    {
        get
        {
            return _pageSize;
        }
        set
        {
            _pageSize = value;
        }
    }
    public object[] GroupByExpressionsStates
    {
        get
        {
            return _groupByExpressionsStates;
        }
        set
        {
            _groupByExpressionsStates = value;
        }
    }
    public object SortExpressionsState
    {
        get
        {
            return _sortExpressionsState;
        }
        set
        {
            _sortExpressionsState = value;
        }
    }
    public string FilterExpression
    {
        get
        {
            return _filterExpression;
        }
        set
        {
            _filterExpression = value;
        }
    }
    public List<ColumnSettings> ColumnSettings
    {
        get
        {
            return _columnSettings;
        }
        set
        {
            _columnSettings = value;
        }
    }
    public List<ColumnSettings> AutoGeneratedColumnSettings
    {
        get
        {
            return _autoColumnSettings;
        }
        set
        {
            _autoColumnSettings = value;
        }
    }
    /// <summary>
    /// Returns the serialized object as string
    /// </summary>
    public override string ToString()
    {
        LosFormatter formatter = new LosFormatter();
        StringWriter writer = new StringWriter();
        formatter.Serialize(writer, this);
        return writer.ToString();
    }
    /// <summary>
    /// Returns the serialized object as byte array
    /// </summary>
    public byte[] ToArray()
    {
        LosFormatter formatter = new LosFormatter();
        MemoryStream stream = new MemoryStream();
        formatter.Serialize(stream, this);
        return stream.ToArray();
    }
    /// <summary>
    /// Gets the GridSettingsCollectionInstance from its serialized string data
    /// </summary>
    /// <param name="data">The object as serialized string data</param>
    public static GridSettingsCollection LoadFromSerializedData(string data)
    {
        LosFormatter formatter = new LosFormatter();
        return (GridSettingsCollection)formatter.Deserialize(data);
    }
    /// <summary>
    /// Gets the GridSettingsCollectionInstance from its serialized byte array
    /// </summary>
    /// <param name="data">The object as serialized byte array</param>
    public static GridSettingsCollection LoadFromSerializedData(byte[] data)
    {
        LosFormatter formatter = new LosFormatter();
        MemoryStream stream = new MemoryStream(data);
        return (GridSettingsCollection)formatter.Deserialize(stream);
    }
}
/// <summary>
/// Represents a collection of grid column settings
/// </summary>
[Serializable]
public class ColumnSettings
{
    private string _uniqueName;
    private int _orderIndex;
    private Unit _width;
    private bool _visible;
    private bool _display;
    private GridKnownFunction _currentFilterFunction;
    private string _currentFilterValue;
    public string UniqueName
    {
        get
        {
            return _uniqueName;
        }
        set
        {
            _uniqueName = value;
        }
    }
    public int OrderIndex
    {
        get
        {
            return _orderIndex;
        }
        set
        {
            _orderIndex = value;
        }
    }
    public Unit Width
    {
        get
        {
            return _width;
        }
        set
        {
            _width = value;
        }
    }
    public bool Visible
    {
        get
        {
            return _visible;
        }
        set
        {
            _visible = value;
        }
    }
    public bool Display
    {
        get
        {
            return _display;
        }
        set
        {
            _display = value;
        }
    }
    public GridKnownFunction CurrentFilterFunction
    {
        get
        {
            return _currentFilterFunction;
        }
        set
        {
            _currentFilterFunction = value;
        }
    }
    public string CurrentFilterValue
    {
        get
        {
            return _currentFilterValue;
        }
        set
        {
            _currentFilterValue = value;
        }
    }
}
''' <summary>
''' Imports and exports settings from a RadGrid instance.
''' </summary>
Public Class GridSettingsPersister
    Public Sub New()
        Me.New(Nothing)
    End Sub
    ''' <summary>
    ''' Initializes an instance of GridSettingsPersister from a RadGrid instance
    ''' </summary>
    ''' <param name="grid">The RadGrid instance to import and exports settings</param>
    Public Sub New(ByVal grid As RadGrid)
        Me.New(grid, GridSettingsType.All)
    End Sub
    ''' <summary>
    ''' Initializes an instance of GridSettingsPersister from a RadGrid instance
    ''' and a collection GridSettingsType values
    ''' </summary>
    ''' <param name="grid">The RadGrid instance to import and exports settings</param>
    ''' <param name="persistedSettings">
    ''' A collection of GridSettingType values specifying the type of grid settings
    ''' to import or export
    ''' </param>
    Public Sub New(ByVal grid As RadGrid, ByVal persistedSettingFlags As GridSettingsType)
        _grid = grid
        _persistedSettingTypes = persistedSettingFlags
        _settings = New GridSettingsCollection()
        _settings.ColumnSettings = New List(Of ColumnSettings)()
        _settings.AutoGeneratedColumnSettings = New List(Of ColumnSettings)()
    End Sub
    Private _grid As RadGrid
    Private _persistedSettingTypes As GridSettingsType
    Private _settings As GridSettingsCollection
    ''' <summary>
    ''' The underlyiong RadGrid instance to import or export settings from
    ''' </summary>
    Public Property Grid() As RadGrid
        Get
            Return _grid
        End Get
        Set(ByVal value As RadGrid)
            _grid = value
        End Set
    End Property
    ''' <summary>
    ''' Gets or sets the GridSettingType flags that specify the grid settings to
    ''' export or import
    ''' </summary>
    Public Overridable Property PersistedSettingTypes() As GridSettingsType
        Get
            Return _persistedSettingTypes
        End Get
        Set(ByVal value As GridSettingsType)
            _persistedSettingTypes = value
        End Set
    End Property
    Protected Overridable Property Settings() As GridSettingsCollection
        Get
            Return _settings
        End Get
        Set(ByVal value As GridSettingsCollection)
            _settings = value
        End Set
    End Property
    ''' <summary>
    ''' Saves the current grid settings and returns the settings serilized to string
    ''' </summary>
    Public Overridable Function SaveSettings() As String
        Return GetSavedSettings().ToString()
    End Function
    ''' <summary>
    ''' Saves the current grid settings and retrieves the underlying
    ''' GridSettingsCollection instance that contains the grid settings
    ''' </summary>
    Public Overridable Function GetSavedSettings() As GridSettingsCollection
        If Grid = Nothing Then
            Throw New NullReferenceException()
        End If
        If IsSettingSpecified(GridSettingsType.Paging) Then
            SavePagingSettings()
        End If
        If IsSettingSpecified(GridSettingsType.Grouping) Then
            SaveGroupByExpressions()
        End If
        If IsSettingSpecified(GridSettingsType.Sorting) Then
            SaveSortExpressions()
        End If
        If IsSettingSpecified(GridSettingsType.Filtering) Then
            SaveFilterExpression()
        End If
        If IsSettingSpecified(GridSettingsType.ColumnSettings) Then
            SaveColumnSettings()
        End If
        Return Settings
    End Function
    Protected Function IsSettingSpecified(ByVal settingType As GridSettingsType) As Boolean
        Return (PersistedSettingTypes And GridSettingsType.All) = GridSettingsType.All OrElse (PersistedSettingTypes And settingType) = settingType
    End Function
    Protected Overridable Sub SavePagingSettings()
        Settings.PageSize = Grid.PageSize
    End Sub
    Protected Overridable Sub SaveGroupByExpressions()
        Settings.GroupByExpressionsStates = New Object(Grid.MasterTableView.GroupByExpressions.Count) {}
        Dim i As Integer = 0
        While i < Settings.GroupByExpressionsStates.Length
            Settings.GroupByExpressionsStates(i) = (DirectCast(Grid.MasterTableView.GroupByExpressions(i), IStateManager)).SaveViewState()
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While
    End Sub
    Protected Overridable Sub SaveSortExpressions()
        Settings.SortExpressionsState = (DirectCast(Grid.MasterTableView.SortExpressions, IStateManager)).SaveViewState()
    End Sub
    Protected Overridable Sub SaveFilterExpression()
        Settings.FilterExpression = Grid.MasterTableView.FilterExpression
    End Sub
    Protected Overridable Sub SaveColumnSettings()
        Settings.ColumnSettings.Clear()
        For Each column As GridColumn In Grid.MasterTableView.Columns
            Settings.ColumnSettings.Add(GetColumnSettings(column))
        Next
        Settings.AutoGeneratedColumnSettings.Clear()
        For Each column As GridColumn In Grid.MasterTableView.AutoGeneratedColumns
            Settings.AutoGeneratedColumnSettings.Add(GetColumnSettings(column))
        Next
    End Sub
    Private Function GetColumnSettings(ByVal column As GridColumn) As ColumnSettings
        Dim colSettings As New ColumnSettings()
        colSettings.UniqueName = column.UniqueName
        colSettings.Width = column.HeaderStyle.Width
        colSettings.Visible = column.Visible
        colSettings.Display = column.Display
        colSettings.OrderIndex = column.OrderIndex
        colSettings.CurrentFilterFunction = column.CurrentFilterFunction
        colSettings.CurrentFilterValue = column.CurrentFilterValue
        Return colSettings
    End Function
    Private Sub SetColumnSettings(ByRef column As GridColumn, ByVal setting As ColumnSettings)
        column.Display = setting.Display
        column.Visible = setting.Visible
        column.HeaderStyle.Width = setting.Width
        column.OrderIndex = setting.OrderIndex
        column.CurrentFilterFunction = setting.CurrentFilterFunction
        column.CurrentFilterValue = setting.CurrentFilterValue
    End Sub
    ''' <summary>
    ''' Loads grids settings from a serialized string
    ''' </summary>
    ''' <param name="value">The string that contains the serialized settings</param>
    Public Overridable Sub LoadSettings(ByVal value As String)
        LoadSettings(GridSettingsCollection.LoadFromSerializedData(value))
    End Sub
    ''' <summary>
    ''' Loads grids settings from a byte array
    ''' </summary>
    ''' <param name="data">The byte array that contains the serialized grid settings</param>
    Public Overridable Sub LoadSettings(ByVal data As Byte())
        LoadSettings(GridSettingsCollection.LoadFromSerializedData(data))
    End Sub
    ''' <summary>
    ''' Loads grid settings from a GridSettingsCollection instance
    ''' </summary>
    ''' <param name="settings">The GridSettingsCollection instance to load settings from</param>
    Public Overridable Sub LoadSettings(ByVal settings As GridSettingsCollection)
        If Grid = Nothing OrElse settings = Nothing Then
            Throw New NullReferenceException()
        End If
        settings = settings
        If IsSettingSpecified(GridSettingsType.Paging) Then
            LoadPagingSettings()
        End If
        If IsSettingSpecified(GridSettingsType.Grouping) Then
            LoadGroupByExpressions()
        End If
        If IsSettingSpecified(GridSettingsType.Sorting) Then
            LoadSortExpressions()
        End If
        If IsSettingSpecified(GridSettingsType.Filtering) Then
            LoadFilterExpression()
        End If
        If IsSettingSpecified(GridSettingsType.ColumnSettings) Then
            LoadColumnSettings()
        End If
    End Sub
    Protected Overridable Sub LoadPagingSettings()
        If Grid.AllowPaging AndAlso Settings.PageSize > 0 Then
            Grid.PageSize = Settings.PageSize
        End If
    End Sub
    Protected Overridable Sub LoadGroupByExpressions()
        If Settings.GroupByExpressionsStates = Nothing Then
            Return
        End If
        Grid.MasterTableView.GroupByExpressions.Clear()
        For Each expressionState As Object In Settings.GroupByExpressionsStates
            Dim expression As New GridGroupByExpression()
       (DirectCast(expression, IStateManager)).LoadViewState(expressionState)
            Grid.MasterTableView.GroupByExpressions.Add(expression)
        Next
    End Sub
    Protected Overridable Sub LoadSortExpressions()
        If Settings.SortExpressionsState = Nothing Then
            Return
        End If
      (DirectCast(Grid.MasterTableView.SortExpressions, IStateManager)).LoadViewState(Settings.SortExpressionsState)
    End Sub
    Protected Overridable Sub LoadFilterExpression()
        Grid.MasterTableView.FilterExpression = Settings.FilterExpression
    End Sub
    Protected Overridable Sub LoadColumnSettings()
        If Settings.AutoGeneratedColumnSettings.Count > 0 Then
            Grid.ColumnCreated += New GridColumnCreatedEventHandler(Grid_ColumnCreated)
        End If
        For Each colSetting As ColumnSettings In Settings.ColumnSettings
            Dim column As GridColumn = Grid.MasterTableView.GetColumnSafe(colSetting.UniqueName)
            If column <> Nothing Then
                SetColumnSettings(column, colSetting)
            End If
        Next
    End Sub
    Private Sub Grid_ColumnCreated(ByVal sender As Object, ByVal e As GridColumnCreatedEventArgs)
      Dim settings As ColumnSettings = Settings.AutoGeneratedColumnSettings.Find(Function([set] As ColumnSettings) Do
        Return [set].UniqueName = e.Column.UniqueName
      End Function)
    Dim column As GridColumn = e.Column
      If settings <> Nothing Then
       SetColumnSettings(column, settings)
      End If
     End Sub
End Class
''' <summary>
''' Enumerates the types of grid settings that can be persisted
''' </summary>
<Flags()> _
Public Enum GridSettingsType
    Paging = 1
    Sorting = 2
    Filtering = 4
    Grouping = 8
    ColumnSettings = 16
    All = 32
End Enum
''' <summary>
''' Represents a collection of grid settings
''' </summary>
<Serializable()> _
Public Class GridSettingsCollection
    Private _pageSize As Integer
    Private _groupByExpressionsStates As Object()
    Private _sortExpressionsState As Object
    Private _filterExpression As String
    Private _columnSettings As List(Of ColumnSettings)
    Private _autoColumnSettings As List(Of ColumnSettings)
    Public Property PageSize() As Integer
        Get
            Return _pageSize
        End Get
        Set(ByVal value As Integer)
            _pageSize = value
        End Set
    End Property
    Public Property GroupByExpressionsStates() As Object()
        Get
            Return _groupByExpressionsStates
        End Get
        Set(ByVal value As Object())
            _groupByExpressionsStates = value
        End Set
    End Property
    Public Property SortExpressionsState() As Object
        Get
            Return _sortExpressionsState
        End Get
        Set(ByVal value As Object)
            _sortExpressionsState = value
        End Set
    End Property
    Public Property FilterExpression() As String
        Get
            Return _filterExpression
        End Get
        Set(ByVal value As String)
            _filterExpression = value
        End Set
    End Property
    Public Property ColumnSettings() As List(Of ColumnSettings)
        Get
            Return _columnSettings
        End Get
        Set(ByVal value As List(Of ColumnSettings))
            _columnSettings = value
        End Set
    End Property
    Public Property AutoGeneratedColumnSettings() As List(Of ColumnSettings)
        Get
            Return _autoColumnSettings
        End Get
        Set(ByVal value As List(Of ColumnSettings))
            _autoColumnSettings = value
        End Set
    End Property
    ''' <summary>
    ''' Returns the serialized object as string
    ''' </summary>
    Public Overloads Overrides Function ToString() As String
        Dim formatter As New LosFormatter()
        Dim writer As New StringWriter()
        formatter.Serialize(writer, Me)
        Return writer.ToString()
    End Function
    ''' <summary>
    ''' Returns the serialized object as byte array
    ''' </summary>
    Public Function ToArray() As Byte()
        Dim formatter As New LosFormatter()
        Dim stream As New MemoryStream()
        formatter.Serialize(stream, Me)
        Return stream.ToArray()
    End Function
    ''' <summary>
    ''' Gets the GridSettingsCollectionInstance from its serialized string data
    ''' </summary>
    ''' <param name="data">The object as serialized string data</param>
    Public Shared Function LoadFromSerializedData(ByVal data As String) As GridSettingsCollection
        Dim formatter As New LosFormatter()
        Return DirectCast(formatter.Deserialize(data), GridSettingsCollection)
    End Function
    ''' <summary>
    ''' Gets the GridSettingsCollectionInstance from its serialized byte array
    ''' </summary>
    ''' <param name="data">The object as serialized byte array</param>
    Public Shared Function LoadFromSerializedData(ByVal data As Byte()) As GridSettingsCollection
        Dim formatter As New LosFormatter()
        Dim stream As New MemoryStream(data)
        Return DirectCast(formatter.Deserialize(stream), GridSettingsCollection)
    End Function
End Class
''' <summary>
''' Represents a collection of grid column settings
''' </summary>
<Serializable()> _
Public Class ColumnSettings
    Private _uniqueName As String
    Private _orderIndex As Integer
    Private _width As Unit
    Private _visible As Boolean
    Private _display As Boolean
    Private _currentFilterFunction As GridKnownFunction
    Private _currentFilterValue As String
    Public Property UniqueName() As String
        Get
            Return _uniqueName
        End Get
        Set(ByVal value As String)
            _uniqueName = value
        End Set
    End Property
    Public Property OrderIndex() As Integer
        Get
            Return _orderIndex
        End Get
        Set(ByVal value As Integer)
            _orderIndex = value
        End Set
    End Property
    Public Property Width() As Unit
        Get
            Return _width
        End Get
        Set(ByVal value As Unit)
            _width = value
        End Set
    End Property
    Public Property Visible() As Boolean
        Get
            Return _visible
        End Get
        Set(ByVal value As Boolean)
            _visible = value
        End Set
    End Property
    Public Property Display() As Boolean
        Get
            Return _display
        End Get
        Set(ByVal value As Boolean)
            _display = value
        End Set
    End Property
    Public Property CurrentFilterFunction() As GridKnownFunction
        Get
            Return _currentFilterFunction
        End Get
        Set(ByVal value As GridKnownFunction)
            _currentFilterFunction = value
        End Set
    End Property
    Public Property CurrentFilterValue() As String
        Get
            Return _currentFilterValue
        End Get
        Set(ByVal value As String)
            _currentFilterValue = value
        End Set
    End Property
End Class

You can extend the logic and store the user preferences via Profile object (part of ASP.NET 2.0). Thus the settings will be preserved not only for the current user session but for subsequent sessions as well. More details are available in this code library thread.

In this article