Click to See Complete Forum and Search --> : [RESOLVED] Line number on a GridView
renatois
10-22-2007, 08:50 AM
Line number on a GridView
Hi all, I have some records on a GridView and I want to show a colunm wih the line number according to the order shown at the GridView.
"One" for the first record, "two" for the second, "three" for the third and so on... it can't come from a field at the data base because itīs dinamic and depends on other fields.
Any solution?
Thank you all
lotuzwine
10-22-2007, 09:12 AM
Try this:
Create a variable for your PAGE called "count" as an page atribute. Then, create an event for your GridView "OnItemDataBound" and program it so you can put the value of your "count" variable in the first column of your GridView. At the end of your OnItemDataBound method, increment your "count" variable (count++).
renatois
10-22-2007, 09:38 AM
Ty lo but iīm a begginer, itīs not totally clear for me. Can u explain better with code examples if possible?
Thanks a lot
lotuzwine
10-22-2007, 10:27 AM
see below:
lotuzwine
10-22-2007, 10:27 AM
Let me see if this can help..
this is the code for your page code behind
.
.
public partial class default: System.Web.UI.Page
{
private int count = 0;
.
.
.
protected void gdvMyGdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
GridViewRow oRow = e.Row;
//This will change the Text for the first Column
oRow.Cells[0].Text = count.ToString();
count++;
}
}
and this is the code for your GridView:
<asp:GridView ID="gdvMyGdv" runat="server" OnRowDataBound="gdvMyGdv_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Row Nr.">
//Only put code here if you want to style this Column
</asp:TemplateField>
.
. //Other Colunms, add your code
.
</Columns>
</asp:GridView>
post back if you have any trouble.
renatois
10-22-2007, 12:28 PM
Ty lo, ur solution worked great. Actually I found a simpler one, have a look:
<asp:gridview id="GridView1" runat="server" allowpaging="true" allowsorting="true"
datasourceid="SqlDataSource1">
<columns>
<asp:templatefield headertext="Row">
<itemtemplate>
<%# Container.DisplayIndex + 1 %>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"
selectcommand="SELECT * FROM [Products]">
</asp:sqldatasource>
thanks a lot
lotuzwine
10-23-2007, 07:19 AM
nice, I didnt knew this one, I will try it out in my own pages. cya arround