7

我想在用户单击DataGridView. 如何确定用户是否在DataGridView(默认情况下为灰色区域)内单击,而不是在Column/ Row/中Cell

4

2 回答 2

10

您可以使用MouseClick事件并对其进行命中测试。

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
    }
}
于 2013-08-01T13:49:33.947 回答
2

要确定用户何时单击了 DataGridView 的空白部分,您将必须处理其MouseUp event.

在这种情况下,您可以对点击位置进行 HitTest 并观察它是否指示HitTestInfo.Nowhere.

例如:

private void myDataGridView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
    //'#See if the left mouse button was clicked
    if (e.Button == MouseButtons.Left) {
        //'#Check the HitTest information for this click location
        if (myDataGridView.HitTest(e.X, e.Y) == HitTestInfo.Nowhere) {
            // Do what you want
        }
    }
}
于 2013-08-01T13:50:15.233 回答