Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何取消选择DataGridView控件中的所有选定行?_.net_Winforms_Datagridview - Fatal编程技术网

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

.net 如何取消选择DataGridView控件中的所有选定行?,.net,winforms,datagridview,.net,Winforms,Datagridview,我想在用户单击DataGridView控件的空白非行部分时取消选择该控件中的所有选定行。 我该怎么做?设置 dgv.CurrentCell = null; 当用户单击dgv的空白部分以取消选择DataGridView中的所有行和单元格时,可以使用: 如果您甚至不希望第一行/单元格显示为选中状态,可以将设置为Nothing/null,这将临时隐藏焦点矩形,直到控件再次接收焦点: myDataGridView.CurrentCell = Nothing 要确定用户何时单击DataGridView

我想在用户单击DataGridView控件的空白非行部分时取消选择该控件中的所有选定行。 我该怎么做?

设置

dgv.CurrentCell = null;

当用户单击dgv的空白部分以取消选择DataGridView中的所有行和单元格时,可以使用:

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

myDataGridView.CurrentCell = Nothing
要确定用户何时单击DataGridView的空白部分,必须处理其MouseUp事件。在这种情况下,您可以单击位置并观察其指示。例如:

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
当然,您也可以对现有DataGridView控件进行子类化,以将所有这些功能组合到一个自定义控件中。您需要像上面显示的那样覆盖它。为了方便起见,我还想提供一个公共的DeselectAll方法,它既调用ClearSelection方法,又将CurrentCell属性设置为Nothing


代码示例在VB.NET中都是任意的,因为问题没有指定语言,如果这不是您的母语,则表示歉意。

谢谢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;
            }
        }

我也遇到了同样的问题,并不是完全靠我自己找到了解决办法,但是有了互联网

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;
    }
}

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

我找到了为什么默认选择第一行,并找到了如何在默认情况下不选择它


默认情况下,我的datagridview是windows窗体上第一个制表位的对象。首先在另一个对象上设置tab stop可能会完全禁用datagrid的tab stop,从而禁用在VB.net中为子对象选择第一行的功能:

    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

这实际上也解决了我的问题。出于某种奇怪的原因,将TabStop属性更改为false似乎会在没有选定DataGridView单元格/索引的情况下加载表单。这不起作用。如果在SelectionChanged事件上设置断点并启用.NET Framework源代码单步执行,则可以看到OnHandleCreated对MakeFirstDisplayedCellCurrentCell的调用。这就是为什么会发生这种情况,并不是因为它可以被切换到。这似乎对我不起作用,我让它工作了,但我的datagridview没有a=HitTestInfo。我无需将它更改为。。。。。如果myDataGridView.HitTeste.X,e.Y是myDataGridView.HitTeste.X,e.Y,那么。。。按预期工作,但无法100%确定其工作原理。实现了第一个HitTest代码。@nora您编写的代码没有任何意义。它本不应该起作用。比较两个参考变量的类型。因此,您要做的就是检查HitTest属性是否返回与…HitTest属性相同类型的值。这就像说如果真=真。@nora继续说,实际上,返回的值类型是HitTestInfo。是一个不可继承的类,其中包含一个名为Nowhere的静态字段。这是您需要比较的字段。我想让你大吃一惊的是HitTestInfo类是DataGridView的一个嵌套类。我已经更新了我的答案,以明确限定它。@TaW如果您指定了“太早”的含义,那么该注释将更有帮助。嗯,我发现,例如,在构造函数中填充DGV后是“太早”。将ClearSelection移动到所示的Form_效果很好。。不确定确切的规则是什么;也许是“布局后”之类的?我加了这张便条,因为在类似的帖子上有很多评论认为“选举”对他们不起作用。因此,把它加入投票率高的答案似乎是个好主意。
    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