Tuesday, 31 January 2012

AutoNumber Column or Auto Number in GridView or DataList ASP.NET

Several times we need to display Auto Number or serial number for displaying 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>
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" 
              AllowPaging="True" AutoGenerateColumns="False"
              DataSourceID="SqlDataSource1" PageSize="6" 
              AlternatingRowStyle-ForeColor="#FFFFFF" >
    <asp:TemplateField HeaderText="Serial Number">
        <%# Container.DataItemIndex + 1 %>
    <asp:BoundField DataField="Name" HeaderText="Name" 
                    SortExpression="Name" />
    <asp:BoundField DataField="Location" HeaderText="Location" 
                    SortExpression="Location" />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [Name], [Location] FROM [Details]">

No comments:

Post a Comment

Note: only a member of this blog may post a comment.