R – Prevent double click on a row divider triggering celldoubleclick event

datagridviewwinforms

In a datagridview with rowheaders visibility set to false and allowusertoresizerow set to true, i need to prevent the celldoubleclick event to trigger if doubleclicked on the rowdivider (Toublearrow of the row resize is visible when the cursor is on the divider).

Thanks

Best Answer

I guess the easiest way would be checking the clicked area of the grid on the CellDoubleClick event itself; the logic would be to return in case rowresizetop or rowresizebottom areas are clicked and continue processing if not. Please check an example below for more details:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    // get mouse coordinates
    Point mousePoint = dataGridView1.PointToClient(Cursor.Position);
    DataGridView.HitTestInfo hitTestInfo = dataGridView1.HitTest(mousePoint.X, mousePoint.Y);
    // need to use reflection here to get access to the typeInternal field value which is declared as internal
    FieldInfo fieldInfo = hitTestInfo.GetType().GetField("typeInternal", 
        BindingFlags.Instance | BindingFlags.NonPublic);
    string value = fieldInfo.GetValue(hitTestInfo).ToString();
    if (value.Equals("RowResizeTop") || value.Equals("RowResizeBottom"))
    {
        // one of resize areas is double clicked; stop processing here      
        return;
    }
    else
    {
        // continue normal processing of the cell double click event
    }
} 

hope this helps, regards

Related Topic