Home SWOT Analysis How Strong U R? Articles About Us
Mahatma Gandhi Jawaharlal Nehru Bagat Singh Kamarajar Indira Gandhi

Oh Indian

In remembrance of the Independance day Aug 15, 2007

This site is dedicated to one and all who sincerely participated in freedom struggle. Let us try to fulfill their ambitions!
Skip Navigation Links

ASP.NET - Sorting Provision in Grid View


It is really wonderful that the paging provision is very simple in Grid View but very much powerful as far as the visitor experience is concerned.

We need to use the following procedure to get the Paging facility to our Gridview.

  1. Choose the GridView. Press F4 to make the properties window visible. Make the AllowSorting property to True.
    Setting AllowSorting Property
  2. Open the GridView Tasks Menu and set SortExpression for each and every field to which, we need to have sorting provision. We will normally set the DataField Name itself in the SortExpression property as shown in the sample below.

    Setting SortExpression Property
  3. We normally use two hidden fields to keep track of the Sorting Field and Sorting Direction. This is necessary. This kind of practice will lead avoid problems in sorting along with Paging. So add two hidden fields namely : hfSortField and hfSortDirection. By default set the value property in these fields to point to the default sorting fields and direction, for example, UserName and ASC.
  4. Make sure to assign the required datasource to the Gridview property through code. If you don't know how to do this, please refere to Listing of Records in Grid View.
  5. Additionally add the following code in the Sorting event of the Gridview.
    Protected Sub gvList_Sorting(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) _
                Handles gvList.Sorting
        hfSortField.Value = e.SortExpression
        hfSortDirection.Value = e.SortDirection
        LoadData()
    End Sub
    
  6. Now the LoadDatA() procedure will have the following changes.
    Private Sub LoadData()
        Try
            Dim Sql As String
            ' Here you may extract records from any
            ' table from your database.
            Sql = "Select * From Users " & _
                  " Order By " & hfSortField.Value & " " & _
                                 hfSortDirection.Value
            Dim dt As DataTable
            dt = GetDataTable(Sql)
    
            ' These following code is importat to bind  
            ' the records to the Grid View.
            gvlist.DataSource = dt
            gvlist.DataBind()
        Catch ex As Exception
            ShowError(ex)
        End Try
    End Sub
    
  7. Run to view the result.
Comments from Users (3)
Shiv 21-Sep-2007 21:20:15 
I m going to implements these good things now ... !





kapil 21-Sep-2007 21:18:14 
Thanks for Making It Simple and easy for Us

Puneet 21-Sep-2007 21:16:47 
Great Articles !
Leave a Reply
Name (Required)  
Mail (will not be published) (required)    
Website