GridView with DataTable as source with paging and sorting

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

<!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>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
       <asp:ScriptManager ID="SM1" runat="server">
       </asp:ScriptManager>
       <div>
           <asp:GridView ID="gv" runat="server"
OnPageIndexChanging="gv_PageIndexChanging" OnSorting="gv_Sorting"
               AllowSorting="true" AllowPaging="True" />
       </div>
   </form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

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

    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dataTable = new DataTable();

        // fill datatable with data
        gv.DataSource = GetSortedData(dataTable, gvSortExpression, gvSortDirection);
        gv.DataBind();

    }
    public String gvSortDirection
    {
        get { return ViewState["SortDirection"] as String ?? "ASC"; }
        set { ViewState["SortDirection"] = value; }
    }
    public String gvSortExpression
    {
        get { return ViewState["SortExpression"] as String ?? ""; }
        set { ViewState["SortExpression"] = value; }
    }

    protected void gv_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView gv = (GridView)sender;
        DataView dv = gv.DataSource as DataView;
        DataTable dataTable = dv.Table;

        gv.DataSource = GetSortedData(dataTable, gvSortExpression, gvSortDirection);
        gv.PageIndex = e.NewPageIndex;
        gv.DataBind();
    }
    protected void gv_Sorting(object sender, GridViewSortEventArgs e)
    {
        GridView gv = (GridView)sender;
        DataView dv = gv.DataSource as DataView;
        DataTable dataTable = dv.Table;
        String sortdir = "";
        if (e.SortExpression != "" & e.SortExpression != null)
        {
            if (gvSortExpression == e.SortExpression)
                gvSortDirection = GetSortDirection();
            else
                gvSortDirection = "ASC";
            gvSortExpression = e.SortExpression;
            gv.EditIndex = -1;
        }
        gv.DataSource = GetSortedData(dataTable, e.SortExpression, gvSortDirection);
        gv.DataBind();
    }
    private String GetSortDirection()
    {
        String newSortDirection = String.Empty;
        switch (gvSortDirection)
        {
            case "DESC":
                newSortDirection = "ASC";
                break;
            case "ASC":
                newSortDirection = "DESC";
                break;
        }
        return newSortDirection;
    }
    private DataView GetSortedData(DataTable dataTable, String SortExpression, String SortDirection)
    {
        dataTable = GetDataSet();
        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);

            if (SortExpression != "" && SortExpression != null)
                dataView.Sort = SortExpression + " " + SortDirection;

            // Add filter - You may add filter here
            return dataView;
        }
        return null;
    }
    protected void gvUsers_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView gv = (GridView)sender;
        DataView dv = gv.DataSource as DataView;
        DataTable dataTable = dv.Table;
        gv.DataSource = GetSortedData(dataTable, gvSortExpression, gvSortDirection);
        gv.EditIndex = e.NewEditIndex;
        gv.DataBind();
    }
    public DataTable GetDataSet()
    {

        DataTable dt = new DataTable("Company");
        DataRow dr;
        dt.Columns.Add(new DataColumn("accountNo", typeof(Int32)));
        dt.Columns.Add(new DataColumn("CompanyName", typeof(string)));
        dt.Columns.Add(new DataColumn("Address", typeof(Int32)));
        for (int i = 0; i <= 10; i++)
        {
            dr = dt.NewRow();
            dr[0] = i;
            dr[1] = "Company" + i + Environment.NewLine + "Title" + i;
            dr[2] = i;
            dt.Rows.Add(dr);
        }

        return dt;
    }
}

Please do not post any spam link in the comment box😊

Post a Comment (0)
Previous Post Next Post