How to check if a row header is clicked in a datagridview when using CellClicked event in C#?

1.2k views Asked by At

I have a datagridview where I pop up a dateTimeSelector on the clicked cell using the dataGridView1_CellClick event. However, if I click on one of the row headers it gives the following error:

System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index'

Here is the code I use for reference:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.Columns[e.ColumnIndex].Name.CompareTo("SalesNr") != 0 && dataGridView1.Columns[e.ColumnIndex].Name.CompareTo("PName") != 0  &&
                 dataGridView1.Columns[e.ColumnIndex].Name.CompareTo("Type") != 0 && e.RowIndex > -1)
            {
                cellRect = dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                dateTimePicker1.Size = new Size(cellRect.Width, cellRect.Height);
                dateTimePicker1.Location = new Point(cellRect.X, cellRect.Y);
                dateTimePicker1.Visible = true;
            }
        }

I would like to have a solution to check whether the clicked cell was a row header. If it is, then I simply will not pop up the dateTimeSelector. How do I do that?

Thanks in advance,

Eda

0

There are 0 answers