i have a gridview control in which paging is enabled, i have a datakey value,i want to find a row index with respect to datakey value ,i have this code,
protected int GetRowIndex(object userID)
{
for(int i = 0l i <= GridView1.DataKey.Count -1 ; i++)
{
if(GridView1.DataKey[i].Value == userID)
{
return i;
}
}
}
but there is problem with this code, that if that user id is not found in that page it will return 0, my question is that how can i change the page index to find row in all pages.i am using gridview own paging.
There is no direct support for this, because the GridView doesn't have all of the DataKeys stored away somewhere for it to search. It keeps only the set of DataKeys that it uses for the current page. Changing the page index does not automatically "load" the DataKeys for that page so that you can search each one.
You will have to solve this by adding a function which, when given a userID, will return the page to display. It might be named GetPageFromUserID(object userID). This function will need to search your data store, using the same sort order, page size, and filter conditions as your grid.
Even with that page index, which you'll set into the GridView.PageIndex property, you'll still have to call DataBind() on the GridView in order to get it to load the rows (and the DataKeys) from the database so that you can use the code you have above to find the row index.