AutoNumber Column In GridView DataList ASP.NET

This example explains how to Add AutoNumber Column In GridView Or DataList In ASP.NET 2.0,3.5,4.0 Using C# And VB.NET. Several times we need to display Auto Number or serial number for Rows records in gridview or other similar controls in ASP.NET.

We can add AutoNumber column by using Container.DataItemIndex property in html markup of the Gridview.



Here's the sample html markup for the page

<title>Auto Number Cloumn in GridView </title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" 
              AllowPaging="True" AutoGenerateColumns="False"
              DataSourceID="SqlDataSource1" PageSize="6" 
              AlternatingRowStyle-BackColor="#006699" 
              AlternatingRowStyle-ForeColor="#FFFFFF" >
    <Columns>
    <asp:TemplateField HeaderText="Serial Number">
    <ItemTemplate>
        <%# Container.DataItemIndex + 1 %>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField DataField="Name" HeaderText="Name" 
                    SortExpression="Name" />
    <asp:BoundField DataField="Location" HeaderText="Location" 
                    SortExpression="Location" />
    </Columns>
    </asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Name], [Location] FROM [Details]">
</asp:SqlDataSource>

Hope this helps

If you like this post than join us or share

18 comments:

Özgür S said...

It is Really helpful. Thank you.


Anonymous said...

U just saved me hours. TQ!!!


ashish said...

This comment has been removed by a blog administrator.


ashish said...

thank dude....u really helped me out frm tension.....tc


p said...

hi its great can u explain with more examples


Anonymous said...

Oui, c'est la rГ©ponse intelligible prematuree achat cialis en ligne


Anonymous said...

Genius man !


Dharshi said...

how to delete row in datatable and bind the datatable into datagrid


Anonymous said...

It was hitherto a subject of declaration to whether or not being had to contain a drift to be lived. It now becomes obvious, on the contrary, that it will-power be lived all the think twice if it has no meaning.


Anonymous said...

And you in the long run get to a consensus, where you get a sense of what in effect ought to be done, and then they entrust it to me and then I draw it. I without fail delineate it in the drift, the theoretical sense.


Anonymous said...

Thank u Very much.........


Anonymous said...

I think I just have been told about this issue
at pub 1 day ago by a mate, but at that moment
it didn't caugh my attention.


Anonymous said...

I think I allready have been told about this issue
at work 1 day ago by a colleague, but at that moment
it didn't caugh my attention.


chan said...

Gr8 Work,
Thanx lol

Ch@n*83


iMU said...

thank you dear friend, it really helped, really appreciate your help.


Anonymous said...

this is really gud...thank u


Anonymous said...

Thank u Very much.........


Anonymous said...

hi thanks for your posts it really helped me a lot.Could u plz do one more favour for me.Plz poat an article abot "how to add a new in GridView without using DataSource Controls. "


Find More Articles