Wednesday, 20 August 2014

Paging and Sorting in GridView

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

<!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></title>
</head>
<body bgcolor="Silver">
    <form id="form1" runat="server">
     <br />
    <h4 style="color: #808000">
        Article by Vithal Wadje</h4>
    <br />
 <%--   <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>--%>
        <asp:GridView ID="GridView1" runat="server"  AllowSorting="true"  CellPadding="6" ForeColor="#333333"
            GridLines="None" PageSize="2" AllowPaging="true" OnPageIndexChanging="Gridpaging" OnSorting="Gridsorting">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerSettings FirstPageText="First" LastPageText="End"
                NextPageText="&amp;gt;z" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <%--<SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />--%>
        </asp:GridView>
  <%--  </ContentTemplate>
    </asp:UpdatePanel>  --%>
    </form>
</body>
</html>
--------------------------------------------------------------Code--------------------------
using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Web;
using System.Data;
public partial class _Default : System.Web.UI.Page
{

    private const string ASCENDING = " ASC";
    private const string DESCENDING = " DESC";
    DataTable dt;
    //private void connection()
    //{
    //    constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
    //    con = new SqlConnection(constr);
    //    con.Open();

    //}

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Bindgrid();

        }
    }

    private void Bindgrid()
    {
        //connection();
        //query = "select *from Employee";//not recommended this i have written just for example,write stored procedure for security
        //com = new SqlCommand(query, con);
        //SqlDataAdapter da = new SqlDataAdapter(com);

        dt = new DataTable();
        dt.Columns.Add("Name");
        dt.Columns.Add("Age");
        dt.Columns.Add("Mobile");
        //dt.Rows.Add(new DataRow()
        DataRow dr = dt.NewRow();
        dr["Name"] = "Dharmendra";
        dr["Age"] = "32";
        dr["Mobile"] = "9015095809";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Rakes";
        dr["Age"] = "32";
        dr["Mobile"] = "9015095809";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Mukes";
        dr["Age"] = "32";
        dr["Mobile"] = "9015095809";
        dt.Rows.Add(dr);
        dr = dt.NewRow();
        dr["Name"] = "Gopal";
        dr["Age"] = "32";
        dr["Mobile"] = "9015095809";
        dt.Rows.Add(dr);
        dt.AcceptChanges();
        //da.Fill(dt);


        ViewState["Paging"] = dt;
        GridView1.DataSource = dt;
        GridView1.DataBind();
        //con.Close();

    }
    protected void Gridpaging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GridView1.DataSource = ViewState["Paging"];
        GridView1.DataBind();


    }

    public SortDirection CurrentSortDirection
    {
        get
        {
            if (ViewState["sortDirection"] == null)
            {
                ViewState["sortDirection"] = SortDirection.Ascending;
            }

            return (SortDirection)ViewState["sortDirection"];
        }
        set
        {
            ViewState["sortDirection"] = value;

        }
    }
    protected void Gridsorting(object sender, GridViewSortEventArgs e)
    {
        string ColumnTosort = e.SortExpression;

        if (CurrentSortDirection == SortDirection.Ascending)
        {
            CurrentSortDirection = SortDirection.Descending;
            SortGridView(ColumnTosort, DESCENDING);
        }
        else
        {
            CurrentSortDirection = SortDirection.Ascending;
            SortGridView(ColumnTosort, ASCENDING);
        }

    }

    private void SortGridView(string sortExpression, string direction)
    {
        //  You can cache the DataTable for improving performance
        //dynamic dt = ViewState["Paging"];
        DataTable dtsort = (DataTable)ViewState["Paging"];// dt;
        DataView dv = new DataView(dtsort);
        dv.Sort = sortExpression + direction;

        GridView1.DataSource = dv;
        GridView1.DataBind();
    }
}

note: when column bound self then use:-
<asp:TemplateField HeaderText="Expiry date" SortExpression="ExpDate">

No comments:

Post a Comment