Tuesday, 18 November 2014

How To Use Grid View Page Index Changing In Asp.Net

Here I Will Explain How to use Page Index/Pagination In Grid View.With various types of paging in ASP.NET. We use C# code to bind the SQL data with a Grid View control and use the following simple steps to make your ASP.NET Grid View control with paging enabled. The Grid View control provides you with an easy way to display the number of items on a page

What is Page Index In Asp.Net?
To show he number of items on a page in Grid View.

C# code to bind the SQL data with a Grid View control and use the following steps.
Create a SQL table




HTML 
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1DEMO.aspx.cs" Inherits="Admin_1DEMO" %>
<!DOCTYPE html>
<html >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        My Demo<br />
        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging"  PageSize="4">
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
            <PagerSettings FirstPageText="" LastPageText="" Mode="NextPrevious" NextPageText="Next" PreviousPageText="Prev" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <RowStyle BackColor="White" ForeColor="#003399" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
           <SortedAscendingCellStyle BackColor="#EDF6F6" />
            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
            <SortedDescendingCellStyle BackColor="#D6DFDF" />
            <SortedDescendingHeaderStyle BackColor="#002876" />
     <Columns>
        <asp:TemplateField HeaderText="Sno">
            <ItemTemplate>
            <span>
                <%#Container.DataItemIndex + 1 %>
            </span>
                </ItemTemplate>
        </asp:TemplateField>
    </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>
C#      

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; using System.Web.Configuration; public partial class Admin_1DEMO : System.Web.UI.Page { SqlConnection con = new SqlConnection(WebConfigurationManager.AppSettings["con"]); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { databind(); } } public void databind() { SqlDataAdapter da = new SqlDataAdapter("select * from DEMO_Blog", con); DataTable dt = new DataTable(); da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView1.PageIndex = e.NewPageIndex; databind(); } }
   
Note
When Adding Grid View Make Sure These Property Should Be changed AllowPaging="True" & PageSize="4"


0 comments::

Post a Comment

Copyright © DotNet-Ashok