Thursday 8 February 2018

How To Use Sorting In GridView In Asp.Net

How To Use Sorting In GridView In Asp.Net
Description:

In this example we explain that how to use Sorting in GridView in Asp.Net C#. or how to sort the data in GridView in Asp.Net using C#. or how to perform sorting in GridView in Asp.Net.or sorting GridView columns in asp.net using C#.

So here we demonstrate that how to create custom sorting for the GridView columns in Asp.Net using C#. below is the code to sort the GridView Data in Asp.Net.
Aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test.aspx.cs" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How To Use Sorting In GridView In Asp.Net</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gv_Employee" runat="server" AutoGenerateColumns="false" AllowSorting="True"
            OnSorting="gv_Employee_Sorting" CellPadding="4" ForeColor="#333333">
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Designation" HeaderText="Designation" SortExpression="Designation" />
                <asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
            </Columns>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
        </asp:GridView>
    </div>
    </form>
</body>
</html>

Aspx.cs: 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {

            BindGridView();

        }

    }



    protected void BindGridView()
    {

        DataTable dtEmployee = new DataTable();

        SqlConnection con = new SqlConnection(@"your connection string");

        SqlDataAdapter da = new SqlDataAdapter("Select Name,Designation,Country from Employee", con);

        con.Open();

        da.Fill(dtEmployee);

        con.Close();



        if (dtEmployee.Rows.Count > 0)
        {

            gv_Employee.DataSource = dtEmployee;

            gv_Employee.DataBind();

            ViewState["dt"] = dtEmployee;

            ViewState["sort"] = "Asc";

        }

    }

    // GridView sorting event

    protected void gv_Employee_Sorting(object sender, GridViewSortEventArgs e)
    {

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

        if (dtEmployee.Rows.Count > 0)
        {

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

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

                ViewState["sort"] = "Desc";

            }

            else
            {

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

                ViewState["sort"] = "Asc";

            }

            gv_Employee.DataSource = dtEmployee;

            gv_Employee.DataBind();

        }

    }
}


4 comments: