How to generate the row number in gridview in asp.net

In this asp.net tutorial you will learn how to generate the row number/serial number in gridview in asp.net. These days it is common requirement to show row number in report so that the user can come to know how many records/rows the gridview have. This tutorial will teach you two methods to generate or display the row number in gridview. Let's have a look over it.

generate row number

How to generate the row number in gridview in asp.net

generate_row_number.aspx

Method 1:-
<asp:GridView ID="GridView1" runat="server" EnableTheming="false" AutoGenerateColumns="false" GridLines="None" Width="100%" CssClass="textTD">

<Columns>
<asp:TemplateField HeaderText="Sr No" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="First Name" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" EnableTheming="false" Text='<%# Bind("first_name")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" EnableTheming="false" Text='<%# Bind("last_name")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

</Columns>
</asp:GridView>

I prefer this method as you don't need to code extra in .cs side, also you don't need to write extra code when you want AllowPaging=”true” in gridview. <%# Container.DataItemIndex + 1 %> does all things.

Method 2:-

generate_row_number.aspx
<asp:GridView ID="GridView1" runat="server" EnableTheming="false" AutoGenerateColumns="false" GridLines="None" Width="100%" CssClass="textTD" OnRowDataBound="GridView1_RowDataBound">

<Columns>
<asp:TemplateField HeaderText="Sr" HeaderStyle-Width="5%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblSerialNo" runat="server" EnableTheming="false"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />

</asp:TemplateField>
<asp:TemplateField HeaderText="First Name" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" EnableTheming="false" Text='<%# Bind("first_name")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

<asp:TemplateField HeaderText="Last Name" HeaderStyle-Width="10%" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" EnableTheming="false" Text='<%# Bind("last_name")%>'></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>

generate_row_number.aspx.cs
public partial class Reports_generate_row_number : System.Web.UI.Page
{
int serialNo=1;
protected void Page_Load(object sender, EventArgs e)
{
//Some code will come here
}

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType == DataControlRowType.DataRow)
{

if (!String.IsNullOrEmpty(((Label)e.Row.FindControl("lblFirstName")).Text))
{
Label lblSerial = (Label)e.Row.FindControl("lblSerialNo");
lblSerial.Text = serialNo.ToString();
serialNo = serialNo+1;

}
}

}

}
This method is little bit lengthy as compare to first method because you have to write code in .cs side as well.

You have to fire the onRowDataBound event of GridView. You have to put a label in the gridview and give some id to that label, in my case it is lblSerialNo and then at .cs side you have to declare a global int variable with default value 1 and then in RowDataBound event you have to increment the value of that global int variable by 1 and then assign it to lblSerialNo and that’s it. This method is useful when you want to write your own method for pagination in gridview. So these are the ways to generate the row number in asp.net using c#.

0 comments: