You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否基于DataGridView隐藏列的值设置行背景色?

解决DataGridView隐藏列后无法设置行背景色的问题

当然可以实现!你遇到的问题根源在于:CellFormatting事件只有当单元格需要被绘制时才会触发,当你把highlight列设为隐藏后,该列的单元格不会触发这个事件,导致你的代码逻辑根本没机会执行。

下面给你两种实用的解决方案,任选其一都能解决问题:

方案一:改用RowPrePaint事件(推荐)

这个事件在行绘制前触发,不管列是否可见,都能访问到当前行的所有数据,包括隐藏列的值。代码逻辑更直接,效率也更高:

Private Sub caselist_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles caselist.RowPrePaint
    Try
        ' 获取当前行的highlight值,注意处理DBNull的情况
        Dim highlightValueObj As Object = caselist.Rows(e.RowIndex).Cells("highlight").Value
        If highlightValueObj Is DBNull.Value Then Return
        
        Dim highlightValue As Integer = Convert.ToInt32(highlightValueObj)
        
        Select Case highlightValue
            Case 4
                caselist.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Aquamarine
                caselist.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Black
            Case 5
                caselist.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightSalmon
            Case Else
                ' 重置为默认样式,避免残留之前的颜色
                caselist.Rows(e.RowIndex).DefaultCellStyle.BackColor = caselist.DefaultCellStyle.BackColor
                caselist.Rows(e.RowIndex).DefaultCellStyle.ForeColor = caselist.DefaultCellStyle.ForeColor
        End Select
    Catch ex As Exception
        ' 不要留空Catch,至少记录错误方便调试
        Console.WriteLine($"设置行样式出错: {ex.Message}")
    End Try
End Sub

方案二:修改原CellFormatting事件逻辑

如果你更习惯用CellFormatting,可以去掉对列索引的判断,每次触发事件时都检查当前行的highlight值(不管是哪个单元格触发的):

Private Sub caselist_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles caselist.CellFormatting
    Try
        ' 获取当前行的highlight值
        Dim highlightValueObj As Object = caselist.Rows(e.RowIndex).Cells("highlight").Value
        If highlightValueObj Is DBNull.Value Then Return
        
        Dim highlightValue As Integer = Convert.ToInt32(highlightValueObj)
        
        Select Case highlightValue
            Case 4
                caselist.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Aquamarine
                caselist.Rows(e.RowIndex).DefaultCellStyle.ForeColor = Color.Black
            Case 5
                caselist.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.LightSalmon
            Case Else
                ' 重置默认样式
                caselist.Rows(e.RowIndex).DefaultCellStyle.BackColor = caselist.DefaultCellStyle.BackColor
                caselist.Rows(e.RowIndex).DefaultCellStyle.ForeColor = caselist.DefaultCellStyle.ForeColor
        End Select
    Catch ex As Exception
        Console.WriteLine($"单元格格式化出错: {ex.Message}")
    End Try
End Sub

额外提示

  1. 如果你的数据源是自定义实体类(而非DataTable),可以直接通过DataBoundItem获取值,更高效:
    Dim item As YourCustomClass = DirectCast(caselist.Rows(e.RowIndex).DataBoundItem, YourCustomClass)
    Dim highlightValue As Integer = item.Highlight
    
  2. 一定要处理DBNull的情况,避免出现转换错误;同时不要留空的Catch块,否则出了问题很难排查。

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:28:51