DataGridView鼠标单击空白区时如何清除选中状态?
解决方案 »
- 关于loginview控件不能显示用户名(求高手!)
- C#菜鸟求解,这个代码是什么意思
- C#如何利用form程序中的button1.Text显示图片
- 如何把当前选中的checkboxlist项,添加到另外一个checkboxlist
- ■■★有经验者请进:有关C#开发嵌入式系统!嵌入式开发必看!
- Stream.Read(NumBytes)里面的参数应该怎么写?
- 急急急!!!菜鸟求指教
- 请教一条sql语句
- DropDownList的问题
- 怎样使用自定义组件里的对象?或者我将数据连接等的东西都放在MDI父窗口,子窗口该怎样使用?
- 报表 怎样不显示上面的工具条
- 注册表查询出来的SubKey数据和实际有差别
DataGridView不可能全部区域刚好被数据行(单元格)填满啊,显示了数据的区域以外的部分就是我说的空白区。
{
DataGridView.CurrentCell=null;
}
public int GetRowIndexAt(int mouseLocation_Y)
{
if (dataGridView1.FirstDisplayedScrollingRowIndex < 0)
{
return -1;
}
if (dataGridView1.ColumnHeadersVisible == true && mouseLocation_Y <= dataGridView1.ColumnHeadersHeight)
{
return -1;
}
int index = dataGridView1.FirstDisplayedScrollingRowIndex;
int displayedCount = dataGridView1.DisplayedRowCount(true);
for (int k = 1; k <= displayedCount; )
{
if (dataGridView1.Rows[index].Visible == true)
{
Rectangle rect = dataGridView1.GetRowDisplayRectangle(index, true); // 取该区域的显示部分区域
if (rect.Top <= mouseLocation_Y && mouseLocation_Y < rect.Bottom)
{
return index;
}
k++;
}
index++;
}
return -1;
}
测试用代码:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (GetRowIndexAt(e.Y) == -1)
{
MessageBox.Show("空白区");
dataGridView1.CurrentCell = null;
}
}
多问一句,DataGridView有没有提供比较方便简单的接口来判断呢,而不必自己写这样的函数进行判断?
if (dgv.Rows.Count > 0)
{ this.dgv.Rows[0].Selected = false; }
只能写在load事件才可用..