RSS

How To Add a Serial No. Column to your GridView or Auto Generate Row Number(Serial No) in GridView

23 Aug

Solution#1(Tested)

<asp:GridView  runat=”server” id=”gvstdudent”>
<Columns>
<asp:TemplateField Header Text=”SR.No”>
<ItemTemplate>
<%#Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<asp:BoundField DataField=”FName” HeaderText=”First Name” />
<asp:BoundField DataField=”LName” HeaderText=”Last Name”/>

</asp:GridView>

Description

Auto Generate Row Number in GridView in asp.net. This Code explains how to add auto generate serial No  in a gridview dynamically using This code.

You need only one Template and write this code in ItemTemplate.

<%# Container.DataItemIndex + 1 %>

Step 1 : Drag GridView from toolbox in  web page.

Step 2 : Open properties of the Gridview and Click on Columns than add Template Column. if you want to set AutoGenerateColumns Property to fasle.

Step 3: Now open .aspx   .cs file(Serverside code behind File) and you can add HeaderTemplate and ItemTemplate of Newly add Template column and add this lines of code inside ItemTemplate Tag

<%# Container.DataItemIndex + 1 %>

After adding template Template column and above code inside itemTemplate tag your TemplateField code will look like this

<asp:TemplateField>
<HeaderTemplate>
Row No.
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>

See the full GridView Code

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” >
<Columns>
<asp:TemplateField>
<HeaderTemplate>
Row No.
</HeaderTemplate>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField=”FName” HeaderText=”First Name” />
<asp:BoundField DataField=”LName” HeaderText=”Last Name”/>
</Columns>
</asp:GridView>

Here is the picture if you run the application

Solution#2

protected

int i = 1;

protected void  gvstdudent 1_RowDataBound(object sender, GridViewRowEventArgs e)

{

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

{

e.Row.Cells[0].Text = Convert.ToString(GridView1.PageIndex * GridView1.PageSize + i);

i++;

}

}

<asp:TemplateField HeaderText=”Sr. No.”>

<ItemTemplate>

<asp:Label ID=”Label1″ runat=”server”></asp:Label>

</ItemTemplate>

</asp:TemplateField>

Thanks………

About these ads
 
1 Comment

Posted by on August 23, 2010 in ASP Dot Net C#

 

One response to “How To Add a Serial No. Column to your GridView or Auto Generate Row Number(Serial No) in GridView

  1. vinay

    October 16, 2013 at 11:11 am

    Hi this is really cool article have a look of this article also

    http://www.dotnetpools.com/2013/10/auto-generate-row-number-in-gridview-in.html

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: