Persisting the Grid Sort Order

Telerik has a method that will allow the persistence of ALL the ASP.NET grid settings... but I had trouble making it work, and it seemed heavy handed. More code that I really wanted. I only wanted to save the grid sort expression, the code below will do that, simply. 

 

Note that saving to the Session would be even simpler, but I wanted it saved in SQL

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not IsPostBack Then
        'retrieve the setting from SQL
        Dim oFPUserSetting As New FPUserSetting("TKQuote", "FollowUpSort", App.UserName, App.ConnectionString)
 
        'if the setting was found...
        If oFPUserSetting.isPopulated Then
            'we're saving the setting using one field, delimeted with a pipe
            Dim oSort() As String = Split(oFPUserSetting.SettingValue, "|")
            Dim strFieldName As String = oSort(0)
            Dim strDirection As String = oSort(1)
 
            Dim expression As New GridSortExpression()
            'set the name
            expression.FieldName = strFieldName
 
            'set the direction
            Select Case strDirection
                Case "Ascending"
                    expression.SortOrder = GridSortOrder.Ascending
                Case "Descending"
                    expression.SortOrder = GridSortOrder.Descending
                Case "None"
                    expression.SortOrder = GridSortOrder.None
            End Select
 
            'add it to the grid
            RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression)
        End If
    End If
End Sub

 

Private Sub RadGrid1_SortCommand(sender As Object, e As GridSortCommandEventArgs) Handles RadGrid1.SortCommand
    'fires when the sort changes
    'get the settings that we want to save
    Dim strSortColumn As String = e.SortExpression
    Dim strNewSortOrder As GridSortOrder = e.NewSortOrder
 
    'this is data access code to save the setting to SQL
    Dim oFPUserSetting As New FPUserSetting("TKQuote", "FollowUpSort", App.UserName, App.ConnectionString)
 
    'format the setting
    oFPUserSetting.SettingValue = String.Format("{0}|{1}", strSortColumn, strNewSortOrder)
 
    'save to SQL
    oFPUserSetting.Save(App.ConnectionString)
End Sub

 

 

 


RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences