Jitesh Byahut 2/25/2014 13951

How To Use Sorting In GridView In Asp.Net

Here we are binding a GridView in Asp.Net and C#.

Points Of Remember:

1. Set GridView's AllowSorting property to True.

2. Fire GridView's Sorting event.

3. Set SortExpression to column value.

4. Use ViewState to store DataTable and current sorting order.

5. Add namespace System.Data and System.Data.SqlClient in your C# page.

<!DOCTYPE html>

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"

         AllowSorting="True" OnSorting="GridView1_Sorting" CellPadding="4" ForeColor="#333333">

            <Columns>

                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />

                <asp:BoundField DataField="Branch" HeaderText="Branch" SortExpression="Branch" />

                <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

            </Columns> 

        <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />       

        <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />

        </asp:GridView>

       

    </div>

    </form>

</body>

</html>

C# Codes :

using System.Data;

using System.Data.SqlClient;

 

public partial class test_GridViewSorting : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        if (!IsPostBack)

        {

            BindGridView();

        }

    }

 

    protected void BindGridView()

    {

        DataTable dt = new DataTable();

        SqlConnection con = new SqlConnection(@"Data Source=JITESH-PC\SQL;Initial

                                                                                     Catalog=db_Student;Integrated Security=True");

        SqlDataAdapter da = new SqlDataAdapter("Select Name,Branch,City from tbl_student", con);

        con.Open();

        da.Fill(dt);

        con.Close();

 

        if (dt.Rows.Count > 0)

        {

            GridView1.DataSource = dt;

            GridView1.DataBind();

            ViewState["dt"] = dt;

            ViewState["sort"] = "Asc";

        }

    }

    // GridView sorting event

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

    {

        DataTable dt1 = (DataTable)ViewState["dt"];

        if (dt1.Rows.Count > 0)

        {

            if (Convert.ToString(ViewState["sort"]) == "Asc")

            {

                dt1.DefaultView.Sort = e.SortExpression + " Desc";

                ViewState["sort"] = "Desc";

            }

            else

            {

                dt1.DefaultView.Sort = e.SortExpression + " Asc";

                ViewState["sort"] = "Asc";

            }

            GridView1.DataSource = dt1;

            GridView1.DataBind();

        }

    }

}

View output :

demo


Please give your feedback for improving this page