DataGridView背景色

  1.     Private Sub DataGridView1_CellPainting(ByVal sender As System.ObjectByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
  2.         'Try
  3.         '    Dim x = e.CellBounds.X + 2
  4.         '    Dim y = e.CellBounds.Y + 2
  5.         '    'DataGridView1.Rows(0).Cells(0).Value = "如果你"
  6.         '    DataGridView1.Columns(0).HeaderText = "如果你"
  7.         '    e.Graphics.DrawString("如果你", e.CellStyle.Font, Brushes.Red, x, y, StringFormat.GenericDefault)
  8.         'Catch ex As Exception
  9.         '    MsgBox(ex.Message)
  10.         'End Try
  11.         'If Me.DataGridView1.Columns("ContactName").Index = _
  12.         'e.ColumnIndex AndAlso e.RowIndex >= 0 Then
  13.         '判断是Contactname  列, 并且不是第一行(标题) 
  14.         Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
  15.             e.CellBounds.Width - 4, e.CellBounds.Height - 4)    '定义绘制区域
  16.         Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor) '定义背景色,这里是从DataGridView中提取的默认样式
  17.         Dim gridBrush As New SolidBrush(Me.DataGridView1.GridColor) ' 定义表格(线框) 色刷
  18.         Dim gridLinePen As New Pen(gridBrush)                 '定义表格(线框) 笔      
  19.         Try
  20.             e.Graphics.FillRectangle(backColorBrush, e.CellBounds)  '填充背景色
  21.             e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
  22.                 e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
  23.                 e.CellBounds.Bottom - 1)
  24.             e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
  25.                 e.CellBounds.Top, e.CellBounds.Right - 1, _
  26.                 e.CellBounds.Bottom)
  27.             '绘制边缘线框
  28.             e.Graphics.DrawRectangle(Pens.Blue, newRect)
  29.             '绘制线框,  这个应该是那种双线的样式,可以屏蔽一个代码然后试试看
  30.             If (e.Value IsNot NothingThen
  31.                 '如果有值, 则绘制值内容
  32.                 e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _
  33.                 Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
  34.                 StringFormat.GenericDefault)
  35.                 '绘制字符串,如果不能转换成字符串,会报异常的,
  36.                 '如果你要绘制多颜色文本,有必要依次处理,或者使用渐变色刷,绘制
  37.             End If
  38.             e.Handled = True '告诉系统,已经手动绘制,不需要再次绘制
  39.         Finally
  40.             gridLinePen.Dispose()
  41.             gridBrush.Dispose()
  42.             backColorBrush.Dispose()
  43.         End Try
  44.         ' End If
  45.     End Sub
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值