Page view counter

displaying gridview row index number

Last post 06-18-2007 3:36 AM by XIII. 6 replies.

Sort Posts:

  • displaying gridview row index number

    05-22-2006, 7:19 AM
    Locked
    • Loading...
    • waters
    • Joined on 05-22-2006, 11:12 AM
    • Posts 3
    • Points 15
    Hello,

    I have a bit of a problem in displaying row index numbers in a GridView. Something like having first column on every row that displays row number  (1, 2, 3 etc). If anyone can help...

    Thanks
  • Re: displaying gridview row index number

    05-22-2006, 12:49 PM
    Locked
    • Loading...
    • limno
    • Joined on 06-10-2005, 7:50 PM
    • Iowa, USA
    • Posts 4,265
    • Points 75,892
    • Moderator
      TrustedFriends-MVPs

    One way to do this by using a new SQL Server 2005 function Row_Number() OVER:

    If you are using SQL Server 2005, you can do something linke this:

    In youe select statement:

     SelectCommand="SELECT Row_Number() OVER(order by id) as rowID, [mycol1], [mycol2], [id] FROM [Table1]"

    In you GridView:

      <asp:BoundField DataField="Rowid" HeaderText="Rowid"  ReadOnly="True" SortExpression="Rowid" />

    You are ready to go.

     

    Limno

  • Re: displaying gridview row index number

    05-22-2006, 2:44 PM
    Locked
    • Loading...
    • limno
    • Joined on 06-10-2005, 7:50 PM
    • Iowa, USA
    • Posts 4,265
    • Points 75,892
    • Moderator
      TrustedFriends-MVPs

    Another try:

    1. Add this your GridView:

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="id"
                DataSourceID="SqlDataSource1" OnRowCreated="GridView1_RowCreated1" >

      <Columns>
                <asp:TemplateField HeaderText="myRowid">
                <ItemTemplate><asp:Label runat="server" />                     
                </ItemTemplate>           
                </asp:TemplateField>

    ...

    </Columns>

    2. Add this to your code page

      Protected Sub GridView1_RowCreated1(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
            If e.Row.RowType = DataControlRowType.DataRow Then

                ' Retrieve the Label from the first column.
                Dim myLabel As Label = CType(e.Row.Cells(0).Controls(0), Label)

                ' Set Label text equal to the rowIndex +1
                myLabel.Text = e.Row.RowIndex.ToString() + 1

            End If
        End Sub

     

    Limno

  • Re: displaying gridview row index number

    05-25-2006, 5:23 AM
    Locked
    • Loading...
    • waters
    • Joined on 05-22-2006, 11:12 AM
    • Posts 3
    • Points 15
    thanks a lot. it worked:)
  • Re: displaying gridview row index number

    05-25-2006, 7:33 AM
    Locked
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 12,638
    • Points 109,630
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    another way to do it is this one in the Columns section of your GridView control:

      
    <asp:TemplateField>
        <ItemTemplate>
            <%# Container.DataItemIndex + 1 %>
        </ItemTemplate>
    </asp:TemplateField>
     Grz, Kris.
  • Re: displaying gridview row index number

    06-18-2007, 12:47 AM
    Locked
    • Loading...
    • strazz
    • Joined on 02-03-2007, 12:46 AM
    • Posts 25
    • Points 36

    Thanks XIII, that was exactly what I needed, nice and clean. 

  • Re: displaying gridview row index number

    06-18-2007, 3:36 AM
    Locked
    • Loading...
    • XIII
    • Joined on 06-30-2002, 11:59 PM
    • Essen, Belgium
    • Posts 12,638
    • Points 109,630
    • ASPInsiders
      Moderator
      TrustedFriends-MVPs

    Hi,

    I also wrote an article about it for the 4 different data controls: repeater, datalist, datagrid and gridview: Autonumbering ASP.NET grid controls.

    Grz, Kris.

Page 1 of 1 (7 items)