.net 如何取消选择 DataGridView 控件中的所有选定行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4314673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:58:50  来源:igfitidea点击:

How to deselect all selected rows in a DataGridView control?

.netwinformsdatagridview

提问by Tino Mclaren

I'd like to deselect all selected rows in a DataGridViewcontrol when the user clicks on a blank (non-row) part of the control.
How can I do this?

DataGridView当用户单击控件的空白(非行)部分时,我想取消选择控件中的所有选定行。
我怎样才能做到这一点?

回答by Cody Gray

To deselect all rows and cells in a DataGridView, you can use the ClearSelectionmethod:

要取消选择 a 中的所有行和单元格DataGridView,可以使用以下ClearSelection方法

myDataGridView.ClearSelection()

If you don't want even the first row/cell to appear selected, you can set the CurrentCellpropertyto Nothing/null, which will temporarily hide the focus rectangle until the control receives focus again:

如果您甚至不希望第一行/单元格显示为选中状态,您可以将该CurrentCell属性设置为Nothing/null,这将暂时隐藏焦点矩形,直到控件再次获得焦点:

myDataGridView.CurrentCell = Nothing

To determine when the user has clicked on a blank part of the DataGridView, you're going to have to handle its MouseUpevent. In that event, you can HitTestthe click location and watch for this to indicate HitTestInfo.Nowhere. For example:

要确定用户何时单击了 的空白部分DataGridView,您将不得不处理其MouseUp事件。在这种情况下,您可以HitTest单击位置并注意这一点以指示HitTestInfo.Nowhere。例如:

Private Sub myDataGridView_MouseUp(ByVal sender as Object, ByVal e as System.Windows.Forms.MouseEventArgs)
    ''# See if the left mouse button was clicked
    If e.Button = MouseButtons.Left Then
        ''# Check the HitTest information for this click location
        If myDataGridView.HitTest(e.X, e.Y) = DataGridView.HitTestInfo.Nowhere Then
            myDataGridView.ClearSelection()
            myDataGridView.CurrentCell = Nothing
        End If
    End If
End Sub

Of course, you could also subclass the existing DataGridViewcontrol to combine all of this functionality into a single custom control. You'll need to override its OnMouseUpmethodsimilar to the way shown above. I also like to provide a public DeselectAllmethod for convenience that both calls the ClearSelectionmethod and sets the CurrentCellproperty to Nothing.

当然,您也可以对现有DataGridView控件进行子类化,以将所有这些功能组合到一个自定义控件中。您需要以类似于上面显示的方式覆盖其OnMouseUp方法DeselectAll为方便起见,我还想提供一个公共方法,既可以调用ClearSelection方法,又可以将CurrentCell属性设置为Nothing.

(Code samples are all arbitrarily in VB.NET because the question doesn't specify a language—apologies if this is not your native dialect.)

(代码示例在 VB.NET 中都是任意的,因为问题没有指定一种语言 - 如果这不是您的母语方言,请道歉。)

回答by Tino Mclaren

Thanks Cody heres the c# for ref:

感谢 Cody 继承了 c# 的参考:

if (e.Button == System.Windows.Forms.MouseButtons.Left)
        {
            DataGridView.HitTestInfo hit = dgv_track.HitTest(e.X, e.Y);
            if (hit.Type == DataGridViewHitTestType.None)
            {
                dgv_track.ClearSelection();
                dgv_track.CurrentCell = null;
            }
        }

回答by David

Set

dgv.CurrentCell = null;

when user clicks on a blank part of the dgv.

当用户点击 dgv 的空白部分时。

回答by Mario

i found out why my first row was default selected and found out how to not select it by default.

我发现了为什么我的第一行被默认选中,并发现了默认情况下如何不选择它。

By default my datagridview was the object with the first tab-stop on my windows form. Making the tab stop first on another object (maybe disabling tabstop for the datagrid at all will work to) disabled selecting the first row

默认情况下,我的 datagridview 是 Windows 窗体上第一个制表位的对象。在另一个对象上首先使制表位停止(可能完全禁用数据网格的制表位将起作用)禁用选择第一行

回答by johnnyontheweb

In VB.net use for the Sub:

在 VB.net 中用于 Sub:

    Private Sub dgv_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv.MouseUp
    ' deselezionare se click su vuoto
    If e.Button = MouseButtons.Left Then
        ' Check the HitTest information for this click location
        If Equals(dgv.HitTest(e.X, e.Y), DataGridView.HitTestInfo.Nowhere) Then
            dgv.ClearSelection()
            dgv.CurrentCell = Nothing
        End If
    End If
End Sub

回答by Mario

i have ran into the same problem and found a solution (not totally by myself, but there is the internet for)

我遇到了同样的问题并找到了解决方案(不完全是我自己,但有互联网)

Color blue  = ColorTranslator.FromHtml("#CCFFFF");
Color red = ColorTranslator.FromHtml("#FFCCFF");
Color letters = Color.Black;

foreach (DataGridViewRow r in datagridIncome.Rows)
{
    if (r.Cells[5].Value.ToString().Contains("1")) { 
        r.DefaultCellStyle.BackColor = blue;
        r.DefaultCellStyle.SelectionBackColor = blue;
        r.DefaultCellStyle.SelectionForeColor = letters;
    }
    else { 
        r.DefaultCellStyle.BackColor = red;
        r.DefaultCellStyle.SelectionBackColor = red;
        r.DefaultCellStyle.SelectionForeColor = letters;
    }
}

This is a small trick, the only way you can see a row is selected, is by the very first column (not column[0], but the one therefore). When you click another row, you will not see the blue selection anymore, only the arrow indicates which row have selected. As you understand, I use rowSelection in my gridview.

这是一个小技巧,您可以看到一行被选中的唯一方法,是通过第一列(不是列 [0],而是因此列)。当您单击另一行时,您将不再看到蓝色选择,只有箭头指示已选择哪一行。如您所知,我在 gridview 中使用了 rowSelection。