[英]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
. 我想提示用户在单击
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
? 如何确定用户是否单击了
DataGridView
内部(默认为灰色区域),而不是Column
/ Row
/ Cell
?
You can use MouseClick
event and do a hit test for it. 您可以使用
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
}
}
To determine when the user has clicked on a blank part of the DataGridView, you're going to have to handle its MouseUp event
. 要确定用户何时单击DataGridView的空白部分,您将不得不处理其
MouseUp event
。
In that event, you can HitTest the click location and watch for this to indicate HitTestInfo.Nowhere
. 在那种情况下,您可以HitTest点击位置并观察此点以指示
HitTestInfo.Nowhere
。
For example: 例如:
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.