kh aafi kh aafi - 1 month ago 33
C# Question

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
}
}

Answer

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
        }
    }
}