禁用数据网格视图中的单元格突出显示

时间:2009-11-16 22:28:01

标签: .net vb.net winforms datagridview

如何在datagridview中禁用Cell Highlighting, 即使我点击单元格也不应该突出显示。

请有任何想法

10 个答案:

答案 0 :(得分:97)

ForeColor / BackColor kludge对我不起作用,因为我有不同颜色的细胞。因此,对于同一地点的任何人,我发现了一种更类似于实际禁用该功能的解决方案。

设置SelectionChanged事件以调用运行ClearSelection

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

答案 1 :(得分:61)

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

这样的事情:

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

答案 2 :(得分:5)

快速进行网络搜索,了解如何使数据网格视图选择不可选择&得到这个(网页)。

在SelectionChanged上调用ClearSelection可以而且确实会导致SelectionChanged事件的双重触发,至少。

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

如果你有更多的代码而不是简单的ClearSelection,我会这么做,你会想要在你的代码完成之后压制这个事件。这是一个例子:

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

答案 3 :(得分:3)

在vb中说:

@media(max-width:480) {
.hide_on_mobile {
   display: none;
}

@media(min-width:480) {
.hide_on_non_mobile {
   display: none;
}

答案 4 :(得分:2)

最快的方法是处理不同颜色的单元格,而不需要重新发送任何事件,这样做会是这样的:

private void dgvMyControl_SelectionChanged(object sender, EventArgs e)
{
    dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.SelectionBackColor = dgvMyControl.SelectedCells(0).Style.DefaultCellStyle.BackColor

}
<德尔> 如果允许多个选择,则需要放入迭代器

(编辑)

实际上,这需要在数据填充时进行。它似乎不适用于选择更改方法。因此,在将数据填充到表格中之后,您需要遍历单元格并更改其选定的背景以匹配其正常背景。像这样的东西(语法可能有点偏,我正在从我的vb代码转换它):

foreach (datarow r in dgv.rows)
{
  foreach (datacell c in r.cells)
  {
     c.Style.SelectionBackColor = c.Style.BackColor
  }
}

答案 5 :(得分:1)

乱七八糟,这也有效,因为我只想在单击一个单元格时更改第二列中的单元格背景颜色:

        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

答案 6 :(得分:1)

Private Sub DataGridView1_SelectionChanged(sender As Object, e As System.EventArgs) Handles DataGridView1.SelectionChanged
    Me.DataGridView1.ClearSelection()
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

答案 7 :(得分:0)

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

答案 8 :(得分:0)

到目前为止,我看到的答案并没有完全满足我的要求,但它们为我指明了正确的方向。就我而言,在绑定到数据源后,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只是为了显示搜索结果。

答案 9 :(得分:0)

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