Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have GridView with several pages and I want to retrieve the row index of the second page.
I am getting this error for the below code that I tried.
Index was out of range.

here my code is:
C#
if (e.CommandName == "Go")
{
    int i = Convert.ToInt32 (e.CommandArgument);
    GridViewRow row = GridViewArtenVerp.Rows[i];
    // some code to retrieve cellvalues
}


Can someone help me?
Thanks in advance.
Posted
Updated 27-Jun-11 1:42am
v2
Comments
Prerak Patel 27-Jun-11 7:44am    
Did you check what is the value of i?
ocean_blue4 27-Jun-11 7:50am    
the value of i is 11, the gridview.pagesize = 10. if I navigate to the second page (next) and choise the second row I get the error

how or what you set on e.CommandArgument are u setting the rowindex values in e.CommandArgument this migth be the case.

thanks
faisal
 
Share this answer
 
Comments
ocean_blue4 27-Jun-11 8:08am    
the commandarg of the Rowbutton is : CommandArgument='<%# DataBinder.Eval(Container, "DataItemIndex") %>'
Your command argument is the index in your datasource, not your gridview. If your page size in the gridview is 10 then the valid range of indices for the control will be between 0 and 9.
If you are trying to select the item with a command argument of 11, then you need to take the modulus of the the index and that will translate it to the correct gridview index. i.e. GridViewRow row = GridViewArtenVerp.Rows[i % 10]; (Note. I would add some validation around this. I never allow for calculations in an index field like I have shown here.)
 
Share this answer
 
Comments
ocean_blue4 27-Jun-11 8:42am    
Marcus, it works! thanks a lot
fjdiewornncalwe 30-Jun-11 23:07pm    
No worries, this is why we participate here.
ControllerSYR 18-Oct-21 13:11pm    
Thanks a lot, your answer has solved my problem
Put a debug point on your code and check i. It seems to be a value for which no row exists.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900