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

18 comments:

  1. It is Really helpful. Thank you.

    ReplyDelete
  2. U just saved me hours. TQ!!!

    ReplyDelete
  3. This comment has been removed by a blog administrator.

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

    ReplyDelete
  5. hi its great can u explain with more examples

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

    ReplyDelete
  7. how to delete row in datatable and bind the datatable into datagrid

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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.

    ReplyDelete
  10. Thank u Very much.........

    ReplyDelete
  11. 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.

    ReplyDelete
  12. 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.

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

    ReplyDelete
  14. this is really gud...thank u

    ReplyDelete
  15. Thank u Very much.........

    ReplyDelete
  16. 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. "

    ReplyDelete