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...
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
waters
Member
15 Points
3 Posts
displaying gridview row index number
May 22, 2006 11:19 AM|LINK
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
limno
All-Star
114648 Points
7592 Posts
Moderator
MVP
Re: displaying gridview row index number
May 22, 2006 04:49 PM|LINK
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.
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
limno
All-Star
114648 Points
7592 Posts
Moderator
MVP
Re: displaying gridview row index number
May 22, 2006 06:44 PM|LINK
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
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm
waters
Member
15 Points
3 Posts
Re: displaying gridview row index number
May 25, 2006 09:23 AM|LINK
XIII
All-Star
175992 Points
21431 Posts
ASPInsiders
Moderator
MVP
Re: displaying gridview row index number
May 25, 2006 11:33 AM|LINK
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.Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!
strazz
Member
36 Points
27 Posts
Re: displaying gridview row index number
Jun 18, 2007 04:47 AM|LINK
Thanks XIII, that was exactly what I needed, nice and clean.
XIII
All-Star
175992 Points
21431 Posts
ASPInsiders
Moderator
MVP
Re: displaying gridview row index number
Jun 18, 2007 07:36 AM|LINK
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.
Interested in Azure, ASP.NET (MVC), jQuery, WCF, EF, MS SQL, ...
Keep the forums clean: report to the moderation team!