1
votes

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? - Jacob Seleznev

1 Answers

0
votes

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