- Private Sub DataGridView1_CellPainting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
- 'Try
- ' Dim x = e.CellBounds.X + 2
- ' Dim y = e.CellBounds.Y + 2
- ' 'DataGridView1.Rows(0).Cells(0).Value = "如果你"
- ' DataGridView1.Columns(0).HeaderText = "如果你"
- ' e.Graphics.DrawString("如果你", e.CellStyle.Font, Brushes.Red, x, y, StringFormat.GenericDefault)
- 'Catch ex As Exception
- ' MsgBox(ex.Message)
- 'End Try
- 'If Me.DataGridView1.Columns("ContactName").Index = _
- 'e.ColumnIndex AndAlso e.RowIndex >= 0 Then
- '判断是Contactname 列, 并且不是第一行(标题)
- Dim newRect As New Rectangle(e.CellBounds.X + 1, e.CellBounds.Y + 1, _
- e.CellBounds.Width - 4, e.CellBounds.Height - 4) '定义绘制区域
- Dim backColorBrush As New SolidBrush(e.CellStyle.BackColor) '定义背景色,这里是从DataGridView中提取的默认样式
- Dim gridBrush As New SolidBrush(Me.DataGridView1.GridColor) ' 定义表格(线框) 色刷
- Dim gridLinePen As New Pen(gridBrush) '定义表格(线框) 笔
- Try
- e.Graphics.FillRectangle(backColorBrush, e.CellBounds) '填充背景色
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, _
- e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, _
- e.CellBounds.Bottom - 1)
- e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, _
- e.CellBounds.Top, e.CellBounds.Right - 1, _
- e.CellBounds.Bottom)
- '绘制边缘线框
- e.Graphics.DrawRectangle(Pens.Blue, newRect)
- '绘制线框, 这个应该是那种双线的样式,可以屏蔽一个代码然后试试看
- If (e.Value IsNot Nothing) Then
- '如果有值, 则绘制值内容
- e.Graphics.DrawString(CStr(e.Value), e.CellStyle.Font, _
- Brushes.Crimson, e.CellBounds.X + 2, e.CellBounds.Y + 2, _
- StringFormat.GenericDefault)
- '绘制字符串,如果不能转换成字符串,会报异常的,
- '如果你要绘制多颜色文本,有必要依次处理,或者使用渐变色刷,绘制
- End If
- e.Handled = True '告诉系统,已经手动绘制,不需要再次绘制
- Finally
- gridLinePen.Dispose()
- gridBrush.Dispose()
- backColorBrush.Dispose()
- End Try
- ' End If
- End Sub
DataGridView背景色
最新推荐文章于 2024-01-04 18:01:51 发布