WinForms - DataGridView - 未选择单元格

Bud*_*Joe 11 .net datagridview winforms

有没有办法让DataGridView没有选择单元格?我注意到即使失去焦点()它至少有一个活跃的细胞.还有其他模式允许这个吗?或其他一些技巧?

Ale*_*ris 10

DataGridView.CurrentCell属性可用于清除焦点矩形.

您可以将此属性(DataGridView.CurrentCell)设置为null以临时删除焦点矩形,但是当控件获得焦点并且此属性的值为null时,它将自动设置为FirstDisplayedCell属性的值.

http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentcell.aspx


Dav*_*all 6

我发现DataGridView.CurrentCell = null在尝试获取请求的行为时,这对我不起作用.

我最终使用的是:

    private void dgvMyGrid_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        if (dgvMyGrid.SelectedRows.Count > 0)
        {
            dgvMyGrid.SelectedRows[0].Selected = false;
        }

        dgvMyGrid.SelectionChanged += dgvMyGrid_SelectionChanged;
    }
Run Code Online (Sandbox Code Playgroud)

它需要在DataBindingComplete事件处理程序中.

附加SelectionChanged事件处理程序的位置不会影响所需的行为,但我将其留在代码片段中,因为我注意到我的需求至少最好只在数据绑定后附加处理程序,这样我就可以避免引发选择更改事件对于每个绑定的项目.


J. *_*and 6

在选择更改事件上将DataGridView.CurrentCell设置为null的问题是不会命中后续事件(如单击).

对我有用的选项是将选择颜色更改为网格颜色.因此,选择将不可见.

RowsDefaultCellStyle.SelectionBackColor = BackgroundColor;
RowsDefaultCellStyle.SelectionForeColor = ForeColor;
Run Code Online (Sandbox Code Playgroud)