Q:

How to disable HeaderText Click in c#? 

I change my header text in datagridview.  The data in the datagridview works fine but when I click the Header Text in datagridview, the error says 'Index was out of range.  Must be non-negative and less than the size of the collection.  Parameter name: index'.  How can I disable the HeaderText click in c#? 
Take note: I don't want to sort the data using the header.  I just wanted to disable it.  
Thanks in advance! 

A:

If there is an issue with the click on the header row, you can use the folliwing code to check the issue
If the click occurs in the Column Header the RowIndex will be -1.  This may be a quick fix:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                MessageBox. Show("Clicked cell");
            }

            else if (e.RowIndex < 0)
            {
                MessageBox. Show(" header Clicked'");

            }
        }

Do things if only row index > 0
And if the problem is with the sorting then follow the following link. 
Disable sorting when clicking DataGridView column header

