在datagridview中禁用单元格突出显示

时间:2022-02-17 13:42:29

How to disable Cell Highlighting in a datagridview, Highlighting should not happen even if I click on the cell.

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

Any thoughts please

任何想法请

8 个解决方案

#1


58  

The only way I've found to "disable" highlighting is to set the SelectionBackColor and the SelectionForeColor in the DefaultCellStyle to the same as the BackColor and ForeColor, respectively. You could probably do this programmatically on the form's Load event, but I've also done it in the designer.

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

Something like this:

是这样的:

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

#2


89  

The ForeColor/BackColor kludge wasn't working for me, because I had cells of different colors. So for anyone in the same spot, I found a solution more akin to actually disabling the ability.

前花色/后花色的组合不适合我,因为我有不同颜色的细胞。因此,对于同一地点的任何人,我发现了一种更类似于禁用该功能的解决方案。

Set the SelectionChanged event to call a method that runs ClearSelection

设置SelectionChanged事件以调用运行ClearSelection的方法

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

#3


4  

Did a quick websearch to find out how to make a datagridview selection non-selectable & got this (web page) hit.

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

Calling ClearSelection on SelectionChanged can and does cause a double firing of the SelectionChanged event, at minimum.

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

The first event is when the cell/row is selected and, of course, the SelectionChanged event is fired. The second firing is when ClearSelection is called as it causes (and logically so!) the selection of the datagridview to (again) changed (to no selection), thus firing SelectionChanged.

第一个事件是选择单元格/行,当然,SelectionChanged事件被触发。第二次触发是当ClearSelection被调用时(逻辑上如此!),datagridview的选择(再次)被更改为(没有选择),因此触发选择被更改。

If you have more code than simply ClearSelection going on, as such I do, you'll want to suppress this event until after your code is done. Here's an example:

如果您有比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;
}

#4


2  

The quickest way to do this to handle cells with different colours, without needing to refire any events, would be to do something like this:

处理不同颜色的细胞,而不需要重新启动任何事件,最快的方法是这样做:

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

}
You will need to put in an iterator if you allow multiple selections

(EDIT)

(编辑)

actually, this needs to be done at time for data population. it doesn't appear to work in the on selection changed method. So after populating the data into the table, you need to iterate through the cells and change their selected background to match their normal background. Something like this (syntax may be a little off, I'm converting it from my vb code):

实际上,这需要在数据填充时进行。在选择改变的方法上似乎没有效果。因此,在将数据填充到表中之后,您需要遍历单元格并更改它们所选的背景以匹配它们的正常背景。类似这样的东西(语法可能有点不对,我把它从vb代码中转换过来):

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

#5


1  

Messing around and this also works, as i only want to change the cell background colour in the 2nd column when a cell is clicked:

这也很有用,因为我只想在点击一个单元格时改变第二列的单元格背景色:

        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

That's it. But if you still want to get clicked row/cell index or to access values:

就是这样。但如果您仍然希望获得单击的行/单元格索引或访问值:

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


1  

in vb speak:

在vb中说:

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

#8


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

#1


58  

The only way I've found to "disable" highlighting is to set the SelectionBackColor and the SelectionForeColor in the DefaultCellStyle to the same as the BackColor and ForeColor, respectively. You could probably do this programmatically on the form's Load event, but I've also done it in the designer.

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

Something like this:

是这样的:

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

#2


89  

The ForeColor/BackColor kludge wasn't working for me, because I had cells of different colors. So for anyone in the same spot, I found a solution more akin to actually disabling the ability.

前花色/后花色的组合不适合我,因为我有不同颜色的细胞。因此,对于同一地点的任何人,我发现了一种更类似于禁用该功能的解决方案。

Set the SelectionChanged event to call a method that runs ClearSelection

设置SelectionChanged事件以调用运行ClearSelection的方法

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

#3


4  

Did a quick websearch to find out how to make a datagridview selection non-selectable & got this (web page) hit.

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

Calling ClearSelection on SelectionChanged can and does cause a double firing of the SelectionChanged event, at minimum.

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

The first event is when the cell/row is selected and, of course, the SelectionChanged event is fired. The second firing is when ClearSelection is called as it causes (and logically so!) the selection of the datagridview to (again) changed (to no selection), thus firing SelectionChanged.

第一个事件是选择单元格/行,当然,SelectionChanged事件被触发。第二次触发是当ClearSelection被调用时(逻辑上如此!),datagridview的选择(再次)被更改为(没有选择),因此触发选择被更改。

If you have more code than simply ClearSelection going on, as such I do, you'll want to suppress this event until after your code is done. Here's an example:

如果您有比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;
}

#4


2  

The quickest way to do this to handle cells with different colours, without needing to refire any events, would be to do something like this:

处理不同颜色的细胞,而不需要重新启动任何事件,最快的方法是这样做:

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

}
You will need to put in an iterator if you allow multiple selections

(EDIT)

(编辑)

actually, this needs to be done at time for data population. it doesn't appear to work in the on selection changed method. So after populating the data into the table, you need to iterate through the cells and change their selected background to match their normal background. Something like this (syntax may be a little off, I'm converting it from my vb code):

实际上,这需要在数据填充时进行。在选择改变的方法上似乎没有效果。因此,在将数据填充到表中之后,您需要遍历单元格并更改它们所选的背景以匹配它们的正常背景。类似这样的东西(语法可能有点不对,我把它从vb代码中转换过来):

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

#5


1  

Messing around and this also works, as i only want to change the cell background colour in the 2nd column when a cell is clicked:

这也很有用,因为我只想在点击一个单元格时改变第二列的单元格背景色:

        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

That's it. But if you still want to get clicked row/cell index or to access values:

就是这样。但如果您仍然希望获得单击的行/单元格索引或访问值:

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


1  

in vb speak:

在vb中说:

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

#8


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