Display DataGrid's RowNumber in Bound column?

  • Thread starter Thread starter D. Shane Fowlkes
  • Start date Start date
D

D. Shane Fowlkes

I have a DataGrid which is populated by a DataBind command and is bound to a
DataSet. It displays nicely on the page and all is well. Is it even
possible to have one of the "columns" filled with the row number of the
actual content from a field??

In other words...something to the effect of:
<asp:BoundColumn HeaderText="Record Number" DataField="<%#
MyDataSet("Budgets").Rows.Index %>"/>
<asp:BoundColumn...real fields....... />

It would be nice if I could display incrementing row numbers in my datagrid.

Thanks!
 
In C#.

Add a template column to your datagrid
<ASP:TemplateColumn><ItemTemplate><ASP:Label ID="lblRowCount" Runat="server"></ASP:Label></ItemTemplate></ASP:TemplateColumn

Then wire the ItemDataBound event of your datagrid
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e

Label lbl = (Label) e.Item.FindControl("lblRowCount")
if(lbl == null) return
lbl.Text = Convert.ToString(e.Item.ItemIndex + 1)


Suresh

----- D. Shane Fowlkes wrote: ----

I have a DataGrid which is populated by a DataBind command and is bound to
DataSet. It displays nicely on the page and all is well. Is it eve
possible to have one of the "columns" filled with the row number of th
actual content from a field?

In other words...something to the effect of
<asp:BoundColumn HeaderText="Record Number" DataField="<%
MyDataSet("Budgets").Rows.Index %>"/><asp:BoundColumn...real fields....... /

It would be nice if I could display incrementing row numbers in my datagrid

Thanks
 
Back
Top