Create Serial no for records in Gridview / How to create a column with autogenerated Serial number of each row.

Well, I am not writing anything new or unknown here but it is new found for me!! Recently I had requirement in my project to show serial no as first column in GridView with all the data for understanding the number of rows in the grid view …

In this case we can take use of container’s DataItemIndex property which is keeping track of the index no of the rows bound to Gridsview

We can make use of it to show serial no. Just simple as follows (Details search local MSDN for “DataItemIndex” )

Consider following code I am using

<asp:GridView ID=”GridView1″ Width=860px Height=600px AllowPaging=”true” OnPageIndexChanging=”GridView1_PageIndexChanging” runat=”server”>

<Columns>

<asp:TemplateField>

<HeaderTemplate>Sr No. </HeaderTemplate>

<ItemTemplate> <%# Container.DataItemIndex + 1 %> </ItemTemplate>

</asp:TemplateField>

</Columns>

</asp:GridView>

We can see the it as follows –

Gridview With Serial No.
Gridview With Serial No.

We can also see its getting reflected irrespective of the Page Index –

Gridview with Page + Sr No.
Gridview with Page + Sr No.
Advertisements

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