ASP.NET: How To: Add a row count column to a GridView, DataList or Repeater
This article will show you how to add a row count column to a GridView. This technique can also be applied to a DataList or a Repeater.
Posted by LazyAssCoder
on Sep 07, 2007
2575 Views
Report Abuse
Tags: GridView, DataList, Repeater
Here are the steps:
- Bound your fields to your DataGrid manually by setting the AutoGenerateColumns attribute of the grid to FALSE.
- Add a TemplateField and ItemTemplate tags between the Columns tag
- Between the ItemTemplate tag, add <%# Container.DataItemIndex + 1 %>
- Add the rest of the fields to be bounded to the grid
Here is a sample:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Count">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="FirstName" HeaderText="First Name" />
<asp:BoundField DataField="LastName" HeaderText="Last Name" />
</Columns>
</asp:GridView>