Click Here to Install Silverlight*
IndiaChange|All Microsoft Sites
MSDN
|Developer Centers|Library|Downloads|How To Buy|Subscribers|My MSDN
 
Generating Sequence number in DataGrid
By Saravana
 
Code Snippet Posted: June 10, 2003
 

In this article, we are going to see how to generate sequence number (row number) in DataGrid. To generate sequence number, we will use the DataGridItem’s ItemIndex property. Code for ASPX is shown below,

<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px" runat="server"
SelectedItemStyle-BackColor="#99ccff" PagerStyle-Visible="False" PageSize="10" ItemStyle-CssClass="tableItem" HeaderStyle-CssClass="tableHeader" HeaderStyle-BackColor="#aaaadd" BorderColor="black" AutoGenerateColumns="False" DataKeyField="CustomerID">

   <Columns>
      <asp:templatecolumn headertext="Row Number">
         <itemtemplate>
            <%# Container.ItemIndex+1 %>
         </itemtemplate>
      </asp:templatecolumn>

            <asp:boundcolumn runat="server" DataField="CustomerID"
                        HeaderText="Customer ID"></asp:boundcolumn>
            <asp:boundcolumn runat="server" DataField="CompanyName"
                        HeaderText="Company Name"></asp:boundcolumn>
            <asp:boundcolumn runat="server" DataField="ContactName"
                        HeaderText="Contact Name"></asp:boundcolumn>
            <asp:boundcolumn runat="server" DataField="Address"
                        HeaderText="Address"></asp:boundcolumn>
            <asp:ButtonColumn runat="server" text="Show Details" visible="False"
                        HeaderText="Select"></asp:ButtonColumn>
   </Columns>

</asp:DataGrid>


Sample code as such is very simple, we will create one extra template column in our DataGrid and bind to it the value of Container.ItemIndex. The itemindex property is zero based so we will add one to it to start from one. So we have sequence number in DataGrid. DataGrid will look like this,

 
 
 

©2009 Microsoft Corporation. All rights reserved. Contact Us |Terms of Use |Trademarks |Privacy Statement
Microsoft