Event problem double click on datagrid

0

Hi, I have a problem that if I double-click on a datagrid header it returns an error in the CellDoubleClick event, I am interested in the event being valid for all the cells except the headers, I tried with e.RowIndex> 0 or e.RowIndex < > -1 but it does not work, any suggestions ??

I leave the code of what I was testing

  private void AgendaSemanal_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{      
 if (AgendaSemanal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Libre" && e.RowIndex!=-1)// tambien probe con e.RowIndex>0
{//instrucciones}

}

I also attach the error that is causing me: This is because e.RowIndex is -1 when I double click on the header but I do not understand why it does not let me evaluate it in the IF

Unhandled exception of type 'System.ArgumentOutOfRangeException' in mscorlib.dll Additional information: The index was out of range. It must be a non-negative value and lower than the size of the collection.

    
asked by fer 31.12.2017 в 07:12
source

1 answer

1

The problem is that when you use the operator && (and) first the operator on the left is evaluated and then, if it is valid, on the right.

In the operator on the left you are trying to access the value of the cell and then, in the one on the right, check that it is not the header row. You should do it the other way around:

private void AgendaSemanal_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{      
     if ( e.RowIndex >= 0 &&
        AgendaSemanal.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() == "Libre")
        {//instrucciones}
}
    
answered by 31.12.2017 / 12:22
source