HOW TO USE SORTING IN GRIDVIEW IN ASP.NET C#

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C#  ASP.Net 
 689  View(s)
Ratings:
Rate this:
I want to sort data  in gridview how will do this. please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: how to use sorting in gridview in asp.net c#

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="Solid" BorderWidth="1px" CellPadding="4" AllowSorting="True" OnSorting="GridView1_Sorting" >

    <Columns>

    <asp:TemplateField HeaderText="Emp_ID" SortExpression="Emp_ID">

    <ItemTemplate>

    <%#Eval("Emp_ID") %>

    </ItemTemplate>

    <ControlStyle Width="100px"></ControlStyle>

    <HeaderStyle Width="100px" />

    <ItemStyle HorizontalAlign="Center" />

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Emp_Name" SortExpression="Emp_Name">

    <ItemTemplate>

    <%#Eval("Emp_Name") %>

    </ItemTemplate>

    <ControlStyle Width="300px"></ControlStyle>

    <HeaderStyle Width="200px" />

    </asp:TemplateField>

    <asp:TemplateField HeaderText="Emp_Sal" SortExpression="Emp_Sal">

    <ItemTemplate>

    <%#Eval("Emp_Sal") %>

    </ItemTemplate>

    <ControlStyle Width="200px"></ControlStyle>

    <HeaderStyle Width="200px" />

    </asp:TemplateField>

    </Columns>

    <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />

    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />

    <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />

    <RowStyle BackColor="White" ForeColor="#330099" />

    <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />

    <SortedAscendingCellStyle BackColor="#FEFCEB" />

    <SortedAscendingHeaderStyle BackColor="#AF0101" />

    <SortedDescendingCellStyle BackColor="#F6F0C0" />

    <SortedDescendingHeaderStyle BackColor="#7E0000" />

    </asp:GridView>

    using System;

    using System.Collections.Generic;

    using System.Configuration;

    using System.Data;

    using System.Data.SqlClient;

    using System.Linq;

    using System.Web;

    using System.Web.UI;

    using System.Web.UI.WebControls;

    namespace Forumasp

    {

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

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    GridView1.DataSource = GetRecord(); //set DataSource GridView

    GridView1.DataBind(); //binding GridView

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)

    {

    GridView1.PageIndex = e.NewPageIndex;

    GridView1.DataSource = GetRecord();

    GridView1.DataBind();

    }

    public DataTable GetRecord()

    {

    DataTable dt = new DataTable();

    string constr = ConfigurationManager.ConnectionStrings["forumConnectionString"].ToString(); // connection string

    SqlConnection con = new SqlConnection(constr);

    con.Open();

    SqlCommand com = new SqlCommand("select *from Employee", con); // table name

    SqlDataAdapter da = new SqlDataAdapter(com);

    DataSet ds = new DataSet();

    da.Fill(ds);

    dt= ds.Tables[0];

    return dt;

    }

    public SortDirection direction

    {

    get

    {

    if (ViewState["directionState"] == null)

    {

    ViewState["directionState"] = SortDirection.Ascending;

    }

    return (SortDirection)ViewState["directionState"];

    }

    set

    {

    ViewState["directionState"] = value;

    }

    }

    protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)

    {

    string sortingDirection = string.Empty;

    if (direction == SortDirection.Ascending)

    {

    direction = SortDirection.Descending;

    sortingDirection = "Desc";

    }

    else

    {

    direction = SortDirection.Ascending;

    sortingDirection = "Asc";

    }

    DataView sortedView = new DataView(GetRecord());

    sortedView.Sort = e.SortExpression + " " + sortingDirection;

    Session["SortedView"] = sortedView;

    GridView1.DataSource = sortedView;

    GridView1.DataBind();

    }

     

    }

    }

      Modified On Nov-18-2015 10:09:06 PM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!