1

I have a winforms screen and the below code in Form_Activated event.

if (genlGrid1.Rows.Count > 0 && genlGrid1.Rows.Count <= genlGridIndex + 1 && (genlGridIndex >= 0 && genlGridIndex < 2))    
{    
    //Looks like below line is where exception occurs    
    genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;     
}

I'm very sure that the grid has 2 rows and 9 cells when this line of code is executed.

When this line of code is executed, I get the error below. No idea what is going on with the grid. What is SetCurrentCellAddressCore and why are we getting this exception??

Can some one please help?

System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: columnIndex at System.Windows.Forms.DataGridView.SetCurrentCellAddressCore(Int32 columnIndex, Int32 rowIndex, Boolean setAnchorCellAddress, Boolean validateCurrentCell, Boolean throughMouseClick) at System.Windows.Forms.DataGridView.SetSelectedCellCoreInternal(Int32 columnIndex, Int32 rowIndex, Boolean selected) at System.Windows.Forms.DataGridViewCell.set_Selected(Boolean value)

1
  • What is the value of genlGridIndex? May 4, 2011 at 3:39

1 Answer 1

0

Should it be

if (genlGrid1.Rows.Count > 0 && 
    genlGrid1.Rows.Count >= genlGridIndex + 1 && 
   (genlGridIndex >= 0 && genlGridIndex < 2))    
{        
    genlGrid1.Rows[genlGridIndex].Cells[0].Selected = true;     
}

?

Note genlGrid1.Rows.Count >= genlGridIndex + 1

1
  • I dont think this is cause of the issue. As we check for genlGridIndex to be either 0 or 1, it will not execute the code in the if condition when genlGridIndex is 0. so it would execute for when genlGridIndex is 1. When its 0, it would not throw exception, instead it would not execute the code. There is some other thing which is cause of the excepti
    – user739207
    May 5, 2011 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.