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

DataGridView添加背景图后,无法显示单元格边框与值的问题求助

解决DataGridView添加背景图后显示网格线及单元格值置顶的问题

方案一:自定义绘制背景与单元格边框

直接通过DGV的绘图事件实现,无需额外控件,逻辑更直接:

  1. 基础样式配置
    先设置DGV及单元格的透明属性,避免默认背景覆盖自定义背景:
Private bgImage As Drawing.Image

Public Sub New()
    InitializeComponent()
    bgImage = Drawing.Image.FromFile("C:\Images\VPBSSquare.Jpg")
    ' 调整背景图尺寸匹配DGV
    bgImage = New Bitmap(bgImage, DataGridView3.Size)
    
    ' 核心透明设置
    DataGridView3.DefaultCellStyle.BackColor = Color.Transparent
    DataGridView3.DefaultCellStyle.SelectionBackColor = Color.Transparent
    DataGridView3.BackgroundColor = Color.Transparent
    ' 关闭表头默认样式,避免遮挡背景
    DataGridView3.EnableHeadersVisualStyles = False
    DataGridView3.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent
    DataGridView3.RowHeadersDefaultCellStyle.BackColor = Color.Transparent
    ' 开启双缓冲消除闪烁
    DataGridView3.DoubleBuffered = True
End Sub
  1. 绘制整体背景图
    在DGV的Paint事件中绘制覆盖整个控件的背景图:
Private Sub DataGridView3_Paint(sender As Object, e As PaintEventArgs) Handles DataGridView3.Paint
    e.Graphics.DrawImage(bgImage, 0, 0, DataGridView3.Width, DataGridView3.Height)
End Sub
  1. 绘制单元格边框与内容
    在CellPainting事件中手动绘制完整边框,再渲染单元格内容:
Private Sub DataGridView3_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView3.CellPainting
    ' 跳过表头行/列
    If e.RowIndex < 0 Or e.ColumnIndex < 0 Then Return
    
    ' 填充透明背景
    e.Graphics.FillRectangle(Brushes.Transparent, e.CellBounds)
    
    ' 绘制完整单元格边框(黑色实线,可自定义颜色粗细)
    Using pen As New Pen(Color.Black, 1)
        e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Right, e.CellBounds.Top)
        e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1)
        e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Top, e.CellBounds.Left, e.CellBounds.Bottom)
        e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom)
    End Using
    
    ' 绘制单元格内容(文本、值)
    e.PaintContent(e.CellBounds)
    
    ' 标记事件已处理,阻止默认绘制覆盖
    e.Handled = True
End Sub

方案二:使用PictureBox作为背景层

通过控件层级调整实现背景显示,需注意透明属性的正确设置:

  1. 布局与控件配置
Private Sub FormLayout()
    Dim bgImageStr As String = "C:\Images\VPBSSquare.Jpg"
    
    ' 配置PictureBox作为背景层
    PictureBox1.Location = New Point(186, 41) ' 与DGV同位置
    PictureBox1.Size = New Size(228, 228)
    PictureBox1.Image = Image.FromFile(bgImageStr)
    PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    Me.Controls.Add(PictureBox1)
    PictureBox1.SendToBack() ' 将背景层置于最底层
    
    ' 配置DGV透明与样式
    DataGridView1.Location = New Point(186, 41)
    DataGridView1.Size = New Size(228, 228)
    DataGridView1.DoubleBuffered = True
    DataGridView1.BackgroundColor = Color.Transparent
    DataGridView1.DefaultCellStyle.BackColor = Color.Transparent
    DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Transparent
    DataGridView1.EnableHeadersVisualStyles = False
    DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent
    
    ' 设置单元格尺寸与内容样式
    For i As Integer = 0 To DataGridView1.Rows.Count - 1
        DataGridView1.Rows(i).Height = 76
    Next i
    For i As Integer = 0 To DataGridView1.Columns.Count - 1
        DataGridView1.Columns(i).Width = 76
        DataGridView1.Columns(i).DefaultCellStyle.Font = New Font("Microsoft Sans Serif", 30, FontStyle.Bold)
        DataGridView1.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    Next i
End Sub
  1. 关键注意事项
  • 不要使用窗体TransparencyKey,会导致全局颜色透明,不符合需求;
  • 必须同时设置DGV本身的BackgroundColor和单元格BackColor为透明,才能让底层PictureBox的背景透出;
  • 开启DoubleBuffered可彻底解决绘制闪烁问题。

常见问题排查

  • 边框不显示:检查CellPainting事件是否绘制了完整的四条边框(之前的代码只绘制了上下边框);
  • 背景图被覆盖:确认DGV及单元格的透明属性设置正确,且Paint事件绘制逻辑优先执行;
  • 闪烁严重:确保开启DoubleBuffered,避免频繁触发重绘。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 20:13:15