My thoughts regarding ASP.NET, C#, programming practices, and more...
reading
You Should Read archives
blogs I read
|
Wednesday, August 24, 2005Get Index of Item Being Bound to DataGrid
Problem
Content copyright ©2003-2006 Tod Birdsall
If I am binding a DataTable to a DataGrid, how do I get the index of the DataRow that is being bound so I can use it at runtime? Solution After some initial searching using Google I thought my solution would be to use the DataGridItem.ItemIndex property. While testing I determined that the DataGridItem.ItemIndex property provides you with the index of the item in relation to the DataGrid. In my case I am using this in a pageable DataGrid. No matter which page the user chooses the record being displayed at the top of the grid always has an index equal to 0. Unfortunately this might by on page 2 where the actual DataRow index of the item is something like 15 (15 items being displayed per page). Following more digging I found that the DataGridItem.DataSetIndex property contained the "The index number of the DataGridItem object from the bound data source.". Bingo! |