Add index in repeater/datalist/datagrid
-
The ItemDataBound of the DataGrid occurs when data is bound to a item in a DataGrid control. The first step is to create an empty TemplateColumn in the DataGrid control as follows: ................. The next step is to specify an event handler for the DataGrid's ItemDataBound using the following event signature. void eventhandlername(object Sender, DataGridItemEventArgs e) { ......................... } This event handler should be mapped to the OnItemDataBound property of the DataGrid control as follows: The DataGridItemEventArgs has a property called Item which is of type DataGridItem and gets the referenced item in the DataGrid when the event happens. DataGridItem has a property called DataSetIndex which gives the index number of the DataGridItem. We will have to display this number within the empty template column as follows: e.Item.Cells[0].Text= e.Item.DataSetIndex + 1; Since the DataSetIndex starts with zero one is added to it for displaying the serial number. Also we will have to check the ItemType and display serial number only if the item type is not a Header or a Footer. if(e.Item.Itemtype != ListItemType.Header && e.Item.Itemtype != ListItemType.Footer) { e.Item.Cells[0].Text= e.Item.DataSetIndex + 1; }
-
I think the following will work as well:
-
I think the following will work as well: