Sorting GridView By Columns Header In Asp.Net Ascending Descending

This is example of how to enable Sorting In GridView By Clicking on Columns Or Headers In Ascending Or Descending Direction In Asp.Net Using C# And VB

Sorting in GridView Asp.Net
If you have Populated GridView With ObjectDataSource or SqlDataSOurce to Insert Update Edit Delete record , then You just have to set AllowSorting property to True and SortExpression property of columns to respective field from database to sort.

Setting these properties changes column names to hyperlink and No coding is required.

Write html source as mentioned below

<asp:GridView ID="gvDetails" runat="server" 
              AutoGenerateColumns="False" 
              onsorting="gvDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
<ItemTemplate>
<asp:Label ID="lblFname" runat="server" Text='<%#Eval("FirstName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
<ItemTemplate>
<asp:Label ID="lblLname" runat="server" Text='<%#Eval("LastName")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location" SortExpression="Location">
<ItemTemplate>
<asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

IF SQLCONNECTION AND SQLCOMMAND IS USED IN CODE BEHIND
If data is populated from database using code behind, then to sortwe need to write custom code to save direction (Ascending or Descending) in ViewState and writecode in Sorting Event of Grid.

Populate and load GridView on Page_Load Event

BindGridView Method fetches data from database and return it as DataTable.

C# CODE
protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvDetails.DataSource = BindGridView();
            gvDetails.DataBind();
        }
    }

    private DataTable BindGridView()
    {
        DataTable dtGrid = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        string strSelect = "SELECT FirstName,LastName,Location FROM Details";
        SqlCommand cmd = new SqlCommand(strSelect, con);
        SqlDataAdapter dAdapter = new SqlDataAdapter(cmd);
        dAdapter.Fill(dtGrid);
        return dtGrid;
    }

VB.NET CODE
Protected Sub Page_Load(sender As Object, e As EventArgs)
 If Not Page.IsPostBack Then
  gvDetails.DataSource = BindGridView()
  gvDetails.DataBind()
 End If
End Sub

Private Function BindGridView() As DataTable
 Dim dtGrid As New DataTable()
 Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
 Dim strSelect As String = "SELECT FirstName,LastName,Location FROM Details"
 Dim cmd As New SqlCommand(strSelect, con)
 Dim dAdapter As New SqlDataAdapter(cmd)
 dAdapter.Fill(dtGrid)
 Return dtGrid
End Function

Create Public Property of SortDirection type and store direction in ViewState.

C# CODE
public SortDirection dir
    {
        get
        {
            if (ViewState["dirState"] == null)
            {
                ViewState["dirState"] = SortDirection.Ascending;
            }
            return (SortDirection)ViewState["dirState"];
        }
        set
        {
            ViewState["dirState"] = value;
        }
    }

VB.NET CODE
Public Property dir() As SortDirection
 Get
  If ViewState("dirState") Is Nothing Then
   ViewState("dirState") = SortDirection.Ascending
  End If
  Return DirectCast(ViewState("dirState"), SortDirection)
 End Get
 Set
  ViewState("dirState") = value
 End Set
End Property

Check Gridview's current direction from ViewState and set new sort direction in Sorting Event.

C# CODE
protected void gvDetails_Sorting(object sender, GridViewSortEventArgs e)
    {
        string sortingDirection = string.Empty;
        if (dir == SortDirection.Ascending)
        {
            dir = SortDirection.Descending;
            sortingDirection = "Desc";
        }
        else
        {
            dir = SortDirection.Ascending;
            sortingDirection = "Asc";
        }
        
        DataView sortedView = new DataView(BindGridView());
        sortedView.Sort = e.SortExpression + " " + sortingDirection;
        gvDetails.DataSource = sortedView;
        gvDetails.DataBind();
    }

VB.NET CODE
Protected Sub gvDetails_Sorting(sender As Object, e As GridViewSortEventArgs)
 Dim sortingDirection As String = String.Empty
 If dir = SortDirection.Ascending Then
  dir = SortDirection.Descending
  sortingDirection = "Desc"
 Else
  dir = SortDirection.Ascending
  sortingDirection = "Asc"
 End If

 Dim sortedView As New DataView(BindGridView())
 sortedView.Sort = Convert.ToString(e.SortExpression) & " " & sortingDirection
 gvDetails.DataSource = sortedView
 gvDetails.DataBind()
End Sub

Download Sample Code


If you like this post than join us or share

8 comments:

Anonymous said...

NICE POST MACHA


Anonymous said...

Nice post....
But when I click on to the next gridview page, sorting is not working.....


Anonymous said...

Very nice I complete my task


Anonymous said...

Nice code


Unknown said...

Hi,
i followed ur sorting program it says could not find column name


Unknown said...

Hi Sir,
i am not getting sorting why it showing error like could not find column name....
please reply


Manish said...

It doesn't work at all..
Checked in Debugging mode, GridView_Sorting is triggered twice back to back. 1st on the click on link and second automatically which makes Desc back to ASC.


Anonymous said...

Nice artical


Find More Articles