Tuesday, 8 April 2014

Paging and shorting in grid view using C#


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

<!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>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="ContactName" HeaderText="Employee Name" SortExpression="ContactName" />
                    <asp:BoundField DataField="CustomerID" HeaderText="Employee ID" SortExpression="CustomerID" />
                    <asp:BoundField DataField="City" HeaderText="Job title" SortExpression="City" />
                    <%--<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>
/*-----------------------------------------------------------------------------------------*/
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;
using System.Configuration;

public partial class gridviewSortPaging : System.Web.UI.Page
{
    string Sort_Direction = "ContactName 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 * from customers";
            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();
    }
}

No comments:

Post a Comment