Can i have a Row Counter in a Bounded DataGrid?

  • Thread starter Thread starter RSB
  • Start date Start date
R

RSB

Hi I Want to Bind the DataGrid with the Collection i have and want to
display a RowCounter 1,2,3,4,... in the First Column.. how to Do this??
Does Datagrid Provides any property etc.etc.??

Thank You

RSB
 
A sneaky way is to databind to a protected int with an increment operator:

<templatecolumn etc..> <%#i++%> </...>

Each time the integer is accessed by the databinding code it increments by
one, which produces the desired effect.

rath.
 
RSB said:
Hi I Want to Bind the DataGrid with the Collection i have and want to
display a RowCounter 1,2,3,4,... in the First Column.. how to Do this??
Does Datagrid Provides any property etc.etc.??

The ItemDataBound event of the DataGrid will be raised once per row in the
grid. You can use the EventArgs to get access to the Item being bound, and
therefore, to the ItemIndex:

private void myGrid_ItemDataBound(object sender, DataGridItemEventArgs e)
{
// e.Item.ItemIndex is the index of the item being DataBound
}

You could add a TemplateColumn as your first column, and it could contain a
Label control. You could set the text of the label to ItemIndex.
 
Back
Top