10

In gridview’s RowDataBound event has e.Row.RowIndex and e.Row.DataItemIndex properties.
Please tell me in easily understandable answer, what is difference between them?
In which situation we should use which one?

Amit
  • 21,570
  • 27
  • 74
  • 94

4 Answers4

8

Use the DataItemIndex property to determine the index of the DataItem in the underlying DataSet.
Use the RowIndex property to determine the index of the GridViewRow object in the Rows collection of a GridView control.

Amit
  • 21,570
  • 27
  • 74
  • 94
  • For a particular row in the girdview the value of both these will be same or different? – Amit May 17 '11 at 15:36
  • 1
    Don't get confused `DataItemIndex` is the property of `DataSet` and `RowIndex` is the property of Giridview. – Amit May 17 '11 at 16:10
  • 2
    They are different if you use paging. Then `DataItemIndex` will return index from the beginning of first page and `RowIndex` will give you index on that specific page. This is very important if you want to use data item in `RowCommand` event. – Episodex May 10 '12 at 14:31
4

e.Row.RowIndex return the index of the row that is currently under binding

e.Row.DataItemIndex contains all the data indexes of the rows that is currently under binding.

Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191
3

DataItemIndex is the index of DataItem in the underlying DataSet. YES

RowIndex is the index of Row in the underlying GridView. YES

But there is big a difference

e.g If your girdview has the page size of 10 rows then your RowIndex is always 0-9 for each page but the DataItemIndex will be different when you will go for other pages such as PageIndex 2,3,4 ... On page 2 the DataItemIndex will be between 10-19 but the RowIndex is still 0-9.

Abdul Saboor
  • 4,079
  • 2
  • 33
  • 25
2

Well the difference could be that "e.Row.DataItemIndex" applies to DataItem only; means This property applies only to data rows where as "e.Row.RowIndex" could be for datarow, header row, etc.

RowIndex is the current visible row in the rendered table. DataItemIndex is the actual item's index; they both will show the index of the record in the set of currently displayed records.

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • For a particular row in the girdview the value of both these will be same or different? – Amit May 17 '11 at 15:36