Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,i have a radgrid whereby on selected index changed ,i show another radgrid.The problem is that i get the following error that
C#
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Below is the line am getting the error.Sometimes i dont get the error but now the error appears everytime,Below is the code:
C#
Line 35:         {
Line 36: 
Line 37:             GridDataItem selectedRow = RadgridStudents.SelectedItems[0] as GridDataItem;


Thats the line am getting the error
Posted
Comments
JoCodes 20-Jan-14 6:42am    
Check whether the Radgrid has rows before using selecteditem.
Member 10398773 20-Jan-14 6:50am    
Yes,when i debug,it brings a count of 1
JoCodes 20-Jan-14 6:57am    
try this in the selectedindex change event

foreach (GridDataItem item in grid.SelectedItems)
{

}
Member 10398773 20-Jan-14 7:12am    
Hi,can yu direct me how to apply this please?
Member 10434230 20-Jan-14 8:15am    
GridDataItem selectedRow;
foreach (GridDataItem item in RadgridStudents.SelectedItems)
{
selectedRow = item as GridDataItem;
}

Whereas, can you again check whether you are getting selected count as 1, otherwise you would not have received this error.

1 solution

Try

C#
void RadgridStudents_SelectedIndexChanged(object sender, EventArgs e)
{
  foreach (GridDataItem item in RadgridStudents.SelectedItems)
  {
    //change the code here according to the value you need
var value = item["name"].Text;
  }
}

or

void RadgridStudents_SelectedIndexChanged(object sender, EventArgs e)
{
 if (RadgridStudents.SelectedItems.Count > 0)
 {
 //change the code here according to the value you need
    var value = RadgridStudents.SelectedValues["Columnname"].ToString();

 }
}
 
Share this answer
 
Comments
Member 10398773 20-Jan-14 23:55pm    
Thanks very much.This has helped me solve it.Cheers sir
JoCodes 20-Jan-14 23:59pm    
You are welcome.Happy to see that you solved the issue.
Member 10398773 21-Jan-14 0:00am    
Am glad.Thanks much .All the best

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