vb.net 在鼠标悬停时更改 datagridview 中选定行的背景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9985594/
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-09 16:02:09  来源:igfitidea点击:

Change backcolor of selected row in datagridview on mouseover

vb.netdatagridview

提问by bluesixty

I am attempting to setup a DataGridView on a form so that the row under the mouse is highlighted. I've got that working with the following, except the currently selected row will not highlight on MouseEnter.

我正在尝试在表单上设置 DataGridView,以便突出显示鼠标下的行。除了当前选定的行不会在 MouseEnter 上突出显示外,我已经使用了以下内容。

The forms contains 4 separate DataGridView and the only row that is highlighted should be the one under the mouse cursor.

表单包含 4 个单独的 DataGridView,唯一突出显示的行应该是鼠标光标下的行。

Private Sub dgvPrjDwgs_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseEnter
        If e.RowIndex > -1 Then
            dgvPrjDwgs.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.BlanchedAlmond
        End If
End Sub

Private Sub dgvPrjDwgs_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseLeave
        If e.RowIndex > -1 Then
            dgvPrjDwgs.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.DimGray
        End If
End Sub

The following pic is with the mouse over a random non-selected row. The beige is the highlight color I want to show.

下图是将鼠标悬停在随机未选择的行上。米色是我想要展示的高光颜色。

Highlight, highlight, you so fine, you so fine you blow my mind!

突出显示,突出显示,你真好,你真好,你让我大吃一惊!

This pic is with the mouse over the currently selected row. I want the backcolor to change to BlanchedAlmond when the mouse is over it.

此图片是将鼠标悬停在当前选定的行上。当鼠标悬停在背景上时,我希望背景色变为 BlanchedAlmond。

Why you no highlight when I want.

为什么你在我想要的时候没有突出显示。

So, I changed thinking and tried using the MouseEnter to make that row the selected one. That works great. But it leaves the row selected when the mouse leaves the datagrid and moves to a different one (bad). I tried setting the selected BackColor to match the non-selected BackColor and now it doesn't highlight at all.

所以,我改变了想法并尝试使用 MouseEnter 使该行成为选定的行。这很好用。但是当鼠标离开数据网格并移动到另一个(坏)时,它会留下选中的行。我尝试设置选定的 BackColor 以匹配未选定的 BackColor,现在它根本不突出显示。

Private Sub dgvPrjDwgs_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseEnter
    dgvPrjDwgs.DefaultCellStyle.SelectionBackColor = Color.BlanchedAlmond
    If e.RowIndex > -1 Then
        dgvPrjDwgs.Rows(e.RowIndex).Selected = True
    End If
End Sub
Private Sub dgvPrjDwgs_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseLeave
    dgvPrjDwgs.DefaultCellStyle.SelectionBackColor = Color.DimGray
End Sub

Help Please :)

请帮忙 :)

采纳答案by bluesixty

Got it to work.

得到它的工作。

I was using DefaultCellStyle instead of RowsDefaultCellStyle. Here is the final code.

我使用的是 DefaultCellStyle 而不是 RowsDefaultCellStyle。这是最终的代码。

Private Sub dgvPrjDwgs_CellMouseEnter(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseEnter
        dgvPrjDwgs.RowsDefaultCellStyle.SelectionBackColor = Color.BlanchedAlmond
        If e.RowIndex > -1 Then
            dgvPrjDwgs.Rows(e.RowIndex).Selected = True
        End If
    End Sub
    Private Sub dgvPrjDwgs_CellMouseLeave(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvPrjDwgs.CellMouseLeave
        dgvPrjDwgs.RowsDefaultCellStyle.SelectionBackColor = Color.DimGray
    End Sub