默认选择第一行,可以通过窗体的Load事件中挪用Datagridview的ClearSelection来打消选择
也可以设置
Datagridview的CurrentCell = null;
还可以将已经选中的行的 dgv.Row[0].Selected=false;
那么如何让用户点击如图箭头所指区域 就打消选择呢?
需要在DataGridView的MouseDown或者MouseClick事件内措置惩罚惩罚
DataGridView dgv = sender as DataGridView;
DataGridViewRow clickedRow = GetClickedRow(dgv, e.Y);
if (clickedRow == null)
{
dgv.ClearSelection();
}
public DataGridViewRow GetClickedRow(DataGridView dgv, int mouseY) { if (dgv.FirstDisplayedScrollingRowIndex < 0) { return null; } if (dgv.ColumnHeadersVisible == true && mouseY <= dgv.ColumnHeadersHeight) { return null; } int index = dgv.FirstDisplayedScrollingRowIndex; int displayedCount = dgv.DisplayedRowCount(true); for (int i = 1; i <= displayedCount; ) { if (dgv.Rows[index].Visible == true) { Rectangle rect = dgv.GetRowDisplayRectangle(index, true); if (rect.Top <= mouseY && mouseY < rect.Bottom) { return dgv.Rows[index]; } i++; } index++; } return null; }
View Code这样做好后 点击红色方框内的区域,就会打消选中的行了
如安在控件掉去焦点后(好比去点击另外DataGridView的时候) 当前这个控件掉去焦点 打消选择
private void dgtWaybillAccountDetialDgv_Leave(object sender, EventArgs e) { DataGridView dgv = sender as DataGridView; dgv.ClearSelection(); }
,