Submit Content

Sponsor



Development


Databases


Server Management


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.

2 Comments   Posted by LazyAssCoder on Sep 07, 2007   2575 Views   Report Abuse
Tags: GridView, DataList, Repeater

Bookmark and Share

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>


Comments
  • maniacpro on Dec 01, 2009 06:47:00 PM
    Nice and thanks for the code.

    <a href="http://emailextractor14.com/">email extractor</a>
  • maniacpro on Dec 01, 2009 06:47:19 PM
    http://www.google.com/

Login or Register to add a comment.