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"


Related Posts:

  • Caching In Asp.Net Caching is the ability to store page output and data in memory for the first time it is requested and later we can quickly retrieve them for multiple, repetitious client requests. It is like keeping a copy of something for l… Read More
  • What is Session State In Asp.Net ASP.NET session state enables you to store and retrieve values for a user as the user navigates ASP.NET pages in a Web application. HTTP is a stateless protocol. This means that a Web server treats each HTTP req… Read More
  • Boxing & UnBoxing In C# C# is a strongly-typed language. Every variable and constant has a type, as does every expression that evaluates to a value. In this article I will share about understanding Boxing and UnBoxing in C#. C# Type System c… Read More
  • View State In Asp.Net View state is the method that the ASP.NET page framework uses to preserve page and control values between round trips. When the HTML markup for the page is rendered, the current state of the page and values… Read More
  • How To Use Cookies ASP.NET. Cookies provide the ability to store small amounts of data on a clients machine. It is often used to store user preferences, session variables, or identity. When the user visits your Web site another time, the application c… Read More

0 comments::

Post a Comment

Copyright © 2025 DotNet-Ashok