Tuesday, 18 November 2014

Auto Generate Row Number in GridView

The GridView control is the successor to the DataGrid and extends it in a number of ways. While developing GridView control in ASP.NET, programmers often requires to display row number in GridView controls.

Auto Generate Row Numbers in Grid View

It is a common requirement that Grid Views to have the first column just be a number identifying the row. Because of doing this implementation, the user can see the number of rows returned by scrolling to the end of the Grid View. By adding this code you can add a row number column to your asp.net Grid View Control.
row-index in gridview
The following source code show how to create a row number column field to your Asp.Net GridView

Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="1DEMO.aspx.cs" Inherits="Admin_1DEMO" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            My Demo<br />
        <asp:GridView ID="GridView1" runat="server" BackColor="White" width="100%" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging"  >
            <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# Source Code
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 * fromDEMO_Blog", con);

        DataTable dt = new DataTable();
        da.Fill(dt);
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    
    protected void GridView1_PageIndexChanging(object sender,&nbsp;GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        databind();
    }
}


0 comments::

Post a Comment

Copyright © DotNet-Ashok