Page

How to implement paging and sorting in asp.net Gridview control?

// Example.aspx //


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Example.aspx.cs" Inherits="_Example" %>  
<!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 runat="server">
    <title>Gridview Paging and Sorting </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <div style="font-size: 20px; font-family: Verdana">
           <u> Gridview Paging and Sorting</u>
            <br />
            <br />
        </div>
        <div>
            <asp:GridView ID="GridVwPagingSorting" runat="server" AutoGenerateColumns="False"
                Font-Names="Verdana" AllowPaging="True" AllowSorting="True" PageSize="5" Width="75%"
                OnPageIndexChanging="PageIndexChanging" BorderColor="#CCCCCC" BorderStyle="Solid"
                BorderWidth="1px" OnSorting="Sorting">
                <AlternatingRowStyle BackColor="#BFE4FF" />
                <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" />
                <HeaderStyle Height="30px" BackColor="#6DC2FF" Font-Size="15px" BorderColor="#CCCCCC"
                    BorderStyle="Solid" BorderWidth="1px" />
                <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid"
                    BorderWidth="1px" />
                <Columns>
                    <asp:BoundField DataField="Emp_Name" HeaderText="Employee Name" SortExpression="Emp_Name" />
                    <asp:BoundField DataField="Emp_id" HeaderText="Employee ID" SortExpression="Emp_id" />
                    <asp:BoundField DataField="Emp_job" HeaderText="Job title" SortExpression="Emp_job" />
                    <asp:BoundField DataField="Emp_Dep" HeaderText="Department" SortExpression="Emp_Dep" />
                </Columns>
            </asp:GridView>
        </div>
        <div style="color: Green; font-weight: bold">
            <br />
            <i>You are viewing page
                <%=GridVwPagingSorting.PageIndex + 1%>
                of
                <%=GridVwPagingSorting.PageCount%>
            </i>
        </div>
    </div>
    </form>
</body>
</html>



// Example.aspx.cs //

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;

public partial class Example : System.Web.UI.Page
{

string Sort_Direction = "Emp_Name ASC";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                  ViewState["SortExpr"] = Sort_Direction;
                  DataView dvEmployee = Getdata();
                  GridVwPagingSorting.DataSource = dvEmployee;
                  GridVwPagingSorting.DataBind();
            }
        }
private DataView Getdata()
        {
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString()))
            {
                DataSet dsEmployee = new DataSet();
                string strSelectCmd = "SELECT [Emp_Name],[Emp_id],[Emp_job],[Emp_Dep],[Emp_Salary] FROM [employee]";
                SqlDataAdapter da = new SqlDataAdapter(strSelectCmd, conn);
                da.Fill(dsEmployee, "Employee");
                DataView dvEmp = dsEmployee.Tables["Employee"].DefaultView;
                dvEmp.Sort = ViewState["SortExpr"].ToString();
                return dvEmp;    
            }
        }

protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridVwPagingSorting.PageIndex = e.NewPageIndex;
            DataView dvEmployee = Getdata();
            GridVwPagingSorting.DataSource = dvEmployee;
            GridVwPagingSorting.DataBind();
        }

protected void Sorting(object sender, GridViewSortEventArgs e)
        {
            string[] SortOrder = ViewState["SortExpr"].ToString().Split(' ');
            if (SortOrder[0] == e.SortExpression)
            {
                if (SortOrder[1] == "ASC")
                {
                    ViewState["SortExpr"] = e.SortExpression + " " + "DESC";
                }
                else
                {
                    ViewState["SortExpr"] = e.SortExpression + " " + "ASC";
                }
            }
            else
            {
                ViewState["SortExpr"] = e.SortExpression + " " + "ASC";
            }
            GridVwPagingSorting.DataSource = Getdata();
            GridVwPagingSorting.DataBind();
        }
}


SQL TABLE








OUTPUT


No comments:

Post a Comment