Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Disable Double Click on the Header of a DataGridView

I want to do some stuff while double click on a Gridview (Row) not single cell. Means on a double click event handler not on MouseDoubleClick event. But i am not able to disable the header column and row double click event..and also want to load data to combobox(ComboBox is on the same form) when i double click on the GridView Row. Help me Please..!!!

private void gvLoadAllData_DoubleClick(object sender, EventArgs e)
{
    if()
    {
        //Do Something
    }
}
like image 645
Af'faq Avatar asked Oct 26 '16 17:10

Af'faq


Video Answer


2 Answers

I already had CellDoubleClick event defined and doing stuff, my issue was that a double click on the header was firing that event and therefore crashing the app.

Taking Disaster's idea, I just added the following to get around that event.

if (e.RowIndex == -1) 
    return;
like image 200
blind Skwirl Avatar answered Sep 29 '22 06:09

blind Skwirl


DoubleClick on Row Header

To handle a double click on row header, handle RowHeaderMouseDoubleClick event of DataGridView:

private void dataGridView1_RowHeaderMouseDoubleClick(object sender, 
    DataGridViewCellMouseEventArgs e)
{
    var rowIndex = e.RowIndex;
    //You handled a double click on row header
    //Do what you need
}

DoubleClick on Column Header

To handle a double click on column header handle ColumnHeaderMouseDoubleClick event of DataGridView:

private void dataGridView1_ColumnHeaderMouseDoubleClick(object sender, 
    DataGridViewCellMouseEventArgs e)
{
    var columnIndex = e.ColumnIndex;
    //You handled a double click on column header
    //Do what you need
}

Using DoubleClick

Also if for any reason you want to use DoubleClick event, here is what you should write:

private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
    var g = sender as DataGridView;
    if (g != null)
    {
        var p = g.PointToClient(MousePosition);
        var hti = g.HitTest(p.X, p.Y);
        if (hti.Type == DataGridViewHitTestType.ColumnHeader)
        {
            var columnIndex = hti.ColumnIndex;
            //You handled a double click on column header
            //Do what you need
        }
        else if (hti.Type == DataGridViewHitTestType.RowHeader)
        {
            var rowIndex = hti.RowIndex;
            //You handled a double click on row header
            //Do what you need
        }
    }
}
like image 33
Reza Aghaei Avatar answered Sep 29 '22 04:09

Reza Aghaei