Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine user has clicked DataGridView but not a Cell

I want to prompt the user to enter new elements into a databound collection when they click in the empty area of a DataGridView. How can I find out if the user has clicked inside of the DataGridView (the grey area by default), but not in a Column/Row/Cell?

like image 798
Joel B Avatar asked Aug 01 '13 13:08

Joel B


1 Answers

You can use MouseClick event and do a hit test for it.

private void dgv_MouseClick(object sender, MouseEventArgs e)
{
    var ht = dgv.HitTest(e.X, e.Y);

    if (ht.Type == DataGridViewHitTestType.None)
    {
         //clicked on grey area
    }
}
like image 200
gzaxx Avatar answered Sep 23 '22 18:09

gzaxx