Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.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_Vb.net_Winforms_Datagridview - Fatal编程技术网

.net 在datagridview中禁用单元格高亮显示

.net 在datagridview中禁用单元格高亮显示,.net,vb.net,winforms,datagridview,.net,Vb.net,Winforms,Datagridview,如何在datagridview中禁用单元格高亮显示, 即使单击单元格,也不应高亮显示 有什么想法吗?我发现“禁用”突出显示的唯一方法是将DefaultCellStyle中的SelectionBackColor和SelectionForeColor分别设置为与BackColor和ForeColor相同。您可能可以在表单的Load事件中以编程方式执行此操作,但我也在设计器中完成了此操作 大概是这样的: Me.DataGridView1.DefaultCellStyle.SelectionBackCo

如何在datagridview中禁用单元格高亮显示, 即使单击单元格,也不应高亮显示


有什么想法吗?

我发现“禁用”突出显示的唯一方法是将
DefaultCellStyle
中的
SelectionBackColor
SelectionForeColor
分别设置为与
BackColor
ForeColor
相同。您可能可以在表单的
Load
事件中以编程方式执行此操作,但我也在设计器中完成了此操作

大概是这样的:

Me.DataGridView1.DefaultCellStyle.SelectionBackColor = Me.DataGridView1.DefaultCellStyle.BackColor
Me.DataGridView1.DefaultCellStyle.SelectionForeColor = Me.DataGridView1.DefaultCellStyle.ForeColor

前景色/背景色的乱七八糟对我不起作用,因为我有不同颜色的细胞。因此,对于同一地点的任何人,我找到了一个更类似于实际禁用该功能的解决方案

设置
SelectionChanged
事件以调用运行
ClearSelection

private void datagridview_SelectionChanged(object sender, EventArgs e)
{
    this.datagridview.ClearSelection();
}

做了一个快速的网页搜索,找出如何使datagridview选择不可选择&点击了这个(网页)

在SelectionChanged上调用ClearSelection至少会导致SelectionChanged事件的双重触发

第一个事件是选择单元格/行时,当然,会触发SelectionChanged事件。 第二次触发是在调用ClearSelection时,因为它导致(逻辑上也是如此!)datagridview的选择(再次)更改(为无选择),从而触发SelectionChanged

如果您有更多的代码,而不是像我这样简单地执行,那么您将希望在代码完成之前抑制此事件。下面是一个例子:

 private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
{
  //suppresss the SelectionChanged event
  this.dgvMyControl.SelectionChanged -= dgvMyControl_SelectionChanged;

  //grab the selectedIndex, if needed, for use in your custom code
  // do your custom code here

  // finally, clear the selection & resume (reenable) the SelectionChanged event 
  this.dgvMyControl.ClearSelection();
  this.dgvMyControl.SelectionChanged += dgvMyControl_SelectionChanged;
}
处理具有不同颜色的单元格而不需要重新填充任何事件的最快方法是执行以下操作:

如果允许多个选择,则需要输入迭代器 (编辑)

实际上,这需要在数据填充时完成。它在“更改选择”方法中似乎不起作用。因此,在将数据填充到表中之后,您需要遍历单元格并更改其选定背景以匹配其正常背景。类似这样(语法可能有点不正确,我正在从vb代码转换它):


搞乱了,这也行得通,因为我只想在单击单元格时更改第2列中的单元格背景颜色:

        Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

    Dim row As Integer = DataGridView1.CurrentCellAddress.Y
    Dim column As Integer = DataGridView1.CurrentCellAddress.X

    If column = 1 Then
        Me.DataGridView1.CurrentCell.Selected = False
        DataGridView1.Item(column, row).Style.BackColor = SelectColour()
    End If

End Sub
就这样。 但如果您仍希望单击行/单元格索引或访问值:

Private Sub DataGridView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseDown
    Dim _ht As DataGridView.HitTestInfo = Me.DataGridView1.HitTest(e.X, e.Y)
    If _ht.Type = DataGridViewHitTestType.Cell Then
        Me.DataGridView1.Rows(_ht.RowIndex).Cells(_ht.ColumnIndex).Value = _
        "RowIndex = " & _ht.RowIndex & ", " & "ColumnIndex = " & _ht.ColumnIndex
    End If
End Sub
用vb语言说:

Private Sub datagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles datagridview1.SelectionChanged
        datagridview1.ClearSelection()
End Sub

到目前为止,我看到的答案并没有给我确切的答案,但它们为我指明了正确的方向。在我的例子中,绑定到数据源后,DGV选择并高亮显示了一个单元格,这是我不想要的。 我只想在用户选择了完整行时突出显示。

过了一会儿,我找到了以下解决方案,对我来说效果很好:

private void datagridview_SelectionChanged(object sender, EventArgs e)
{       
    var dgv = (DataGridView)sender;
    if (dgv.SelectedCells.Count == 1)
    {   // hide selection for the single cell
        dgv.DefaultCellStyle.SelectionBackColor = dgv.DefaultCellStyle.BackColor;
        dgv.DefaultCellStyle.SelectionForeColor = dgv.DefaultCellStyle.ForeColor;
    }
    else
    {   // show the selected cells
        dgv.DefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.SelectionBackColor;
        dgv.DefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.SelectionForeColor;
    };
}
注意:在我的示例中,我已经设置了属性

MultiSelect=false,ReadOnly=true

因为我使用DGV只是为了显示搜索结果。


<DataGrid ItemsSource="{Binding Credits}" x:Name="Grid"
                          HorizontalAlignment="Left" RowBackground="Transparent">

问题是vb.net需要进行微小的修改,但效果很好。非常感谢。我不明白为什么控件上没有一个简单的属性可以打开或关闭。我希望“ClearSelection()”再次触发“SelectionChanged”-事件。。。。。但事实并非如此。奇怪。如果某些单元格的颜色不同,则默认背景色无效。如果在行中循环设置颜色,也可以在同一循环中设置选择颜色。那么它就可以正常工作了。更一般地说,您可以在任何时候设置正常颜色时设置选择颜色。我真的不喜欢这样,我必须使用它,但它工作得很好:)
Private Sub datagridview1_SelectionChanged(sender As Object, e As EventArgs) Handles datagridview1.SelectionChanged
        datagridview1.ClearSelection()
End Sub
Private Sub DGW2_DataBindingComplete(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewBindingCompleteEventArgs) Handles DGW2.DataBindingComplete
    Dim mygrid As DataGridView
    mygrid = CType(sender, DataGridView)
    mygrid.ClearSelection()
End Sub
private void datagridview_SelectionChanged(object sender, EventArgs e)
{       
    var dgv = (DataGridView)sender;
    if (dgv.SelectedCells.Count == 1)
    {   // hide selection for the single cell
        dgv.DefaultCellStyle.SelectionBackColor = dgv.DefaultCellStyle.BackColor;
        dgv.DefaultCellStyle.SelectionForeColor = dgv.DefaultCellStyle.ForeColor;
    }
    else
    {   // show the selected cells
        dgv.DefaultCellStyle.SelectionBackColor = dgv.RowsDefaultCellStyle.SelectionBackColor;
        dgv.DefaultCellStyle.SelectionForeColor = dgv.RowsDefaultCellStyle.SelectionForeColor;
    };
}
<DataGrid ItemsSource="{Binding Credits}" x:Name="Grid"
                          HorizontalAlignment="Left" RowBackground="Transparent">