DataGridView SelectLine颜色为无?

Pat*_*ins 2 c# datagridview winforms

网格正确显示所有信息,在事件dataGridView1cellFormatting中,我根据线值下的对象改变背景颜色.这也有效.我在网格上的最后一个事件是dataGridView1_CellPainting,它检查它是否是添加图标的标题.

一切都很好,直到我尝试取出所选行的颜色(或单元格做同样的事情).我想要的是取出所选行的颜色.我试图用"透明"设置它,但是当控件绑定数据时,该行是灰色的,当我们调整列大小时,文本是不可读的.

如何在不突出显示所选行的情况下在DataGridView中显示数据?

Ros*_*ard 5

您可以将SelectionForeColorand SelectionBackColor属性设置为您想要更改高亮颜色的颜色.这可以DefaultCellStyle在DataGridView 上的属性上设置,也可以在单个单元本身上设置.这样,选择行时颜色不会改变.

Private Sub dgv_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgv.CellFormatting
    If e.RowIndex < 0 Then Exit Sub

    If e.RowIndex Mod 2 = 0 Then
        e.CellStyle.BackColor = Color.Orange
    Else
        e.CellStyle.BackColor = Color.Red
    End If

    'Make the selected cell the same color
    e.CellStyle.SelectionBackColor = e.CellStyle.BackColor
    e.CellStyle.SelectionForeColor = e.CellStyle.ForeColor
End Sub
Run Code Online (Sandbox Code Playgroud)