Saturday, November 21, 2015

How to apply pagin and sorting in gridview in asp.net

For applying paging and shorting follow some steps.
Step No -

First we will create a database Demodatabase.
Now create table Demo.


3. Now I am going for design part<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridview_controls.aspx.cs" Inherits="gridview_controls" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></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="#336666" BorderStyle="Double" BorderWidth="3px" OnSorting="Sorting" BackColor="White" CellPadding="4" GridLines="Horizontal"> <PagerStyle BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" BackColor="#336666" ForeColor="White" HorizontalAlign="Center" /> <FooterStyle BackColor="White" ForeColor="#333333" /> <HeaderStyle Height="30px" BackColor="#336666" Font-Size="15px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" Font-Bold="True" ForeColor="White" /> <RowStyle Height="20px" Font-Size="13px" BorderColor="#CCCCCC" BorderStyle="Solid" BorderWidth="1px" BackColor="White" ForeColor="#333333" /> <Columns> <asp:BoundField DataField="Emp_id" HeaderText="ID" SortExpression="Emp_id" /> <asp:BoundField DataField="Emp_Name" HeaderText="Name" SortExpression="Emp_Name" /> <asp:BoundField DataField="Emp_job" HeaderText="Title" SortExpression="Emp_job" /> <asp:BoundField DataField="Emp_Dep" HeaderText="Department" SortExpression="Emp_Dep" /> </Columns> <SelectedRowStyle BackColor="#339966" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F7F7F7" /> <SortedAscendingHeaderStyle BackColor="#487575" /> <SortedDescendingCellStyle BackColor="#E5E5E5" /> <SortedDescendingHeaderStyle BackColor="#275353" /> </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>

4. Code behindusing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Web.UI.WebControls; public partial class gridview_controls : 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 * 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, System.Web.UI.WebControls.GridViewPageEventArgs e) { GridVwPagingSorting.PageIndex = e.NewPageIndex; DataView dvEmployee = Getdata(); GridVwPagingSorting.DataSource = dvEmployee; GridVwPagingSorting.DataBind(); } protected void Sorting(object sender, System.Web.UI.WebControls.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(); } }

5. Now Debug

What do you think ?


Give me feedback by comment.


I hope it is helpfull for you.

No comments:

Post a Comment

Rewrite whole Web Site System and Sub System with data migration to new system.

Can you please share me your skype id or whats up number for better communications  my skype id is - jitendra.tech  whats up - +919617741414...