The Row Number can be auto generated in the following ways.
1. Using Method
2. Using DataItemIndex.
3. Using RowDataBound
event.
Step 1:- (Default.aspx)
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title> Auto Generate Row
Number in GridView</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h3>Using
Public Method</h3>
<asp:GridView ID="GridView1" runat="server" GridLines="Both" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Serial
Number">
<ItemTemplate>
<%# GetSerialNumber() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<h3>Using
Container.DataItemIndex property</h3>
<asp:GridView ID="GridView2" runat="server" GridLines="Both" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Serial
Number">
<ItemTemplate>
<%# Container.DataItemIndex
+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<h3>Using
RowDataBound event</h3>
<asp:GridView ID="GridView3" runat="server" GridLines="Both" OnRowDataBound="GridView3_OnRowDataBound" CellPadding="4" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Serial
Number">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
</div>
</form>
</body>
</html>
Step 2:- (Default.aspx.cs)
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Default : Page
{
private int _num
= 0;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindDataToGridView();
}
}
protected void BindDataToGridView()
{
DataTable dt
= new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
int totalRow
= 5;
for (int i =
0; i < totalRow; i++)
{
dr
= dt.NewRow();
dr["Column1"] = "Row" + i;
dr["Column2"] = "Row" + i;
dr["Column3"] = "Row" + i;
dr["Column4"] = 1000 /
(i + 1);
dt.Rows.Add(dr);
}
GridView1.DataSource
= dt;
GridView1.DataBind();
GridView2.DataSource
= dt;
GridView2.DataBind();
GridView3.DataSource
= dt;
GridView3.DataBind();
}
public int GetSerialNumber()
{
_num
+= 1;
return _num;
}
protected void GridView3_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType
== DataControlRowType.DataRow)
{
var literal
= e.Row.FindControl("Literal1") as Literal;
if (literal
!= null)
literal.Text
= (e.Row.RowIndex + 1).ToString();
}
}
}
}
Step 3:-
Summary
Using Method
private int _num
= 0;
public int GetSerialNumber()
{
_num
+= 1;
return _num;
}
<Columns>
<asp:TemplateField HeaderText="Serial
Number">
<ItemTemplate>
<%# GetSerialNumber() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Using DataItemIndex
<Columns>
<asp:TemplateField HeaderText="Serial
Number">
<ItemTemplate>
<%# Container.DataItemIndex
+1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Using RowDataBound event
if (e.Row.RowType == DataControlRowType.DataRow)
{
var literal
= e.Row.FindControl("Literal1") as Literal;
if (literal
!= null)
literal.Text
= (e.Row.RowIndex + 1).ToString();
}
<Columns>
<asp:TemplateField HeaderText="Serial
Number">
<ItemTemplate>
<asp:Literal ID="Literal1" runat="server"></asp:Literal>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Example:-
No comments:
Post a Comment