日期:2014-05-18  浏览次数:21169 次

怎样才能改变datagridview表头颜色?
如题:
datagridview表头默认颜色为灰白色,怎样才改变?

ColumnHeadersDefaultCellStyle.BackColor 设置了不行?


------解决方案--------------------
VB.NET code

dim mLinearColor1 As Color = Color.FromArgb(248, 250, 251)
dim mLinearColor2 As Color = Color.FromArgb(196, 207, 224)
dim mGridColor As Color = Color.FromArgb(120, 147, 191)    '网格线的颜色
dim mBorderColor As Color = Color.MediumBlue               '控件边框的颜色
dim mMouseInColor As Color = Color.Orange                  '鼠标进入时控件的颜色
dim mHasFocusedColor As Color = Color.DarkCyan             '控件的焦点框颜色 

Private Sub MyDataGridView_CellPainting(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellPaintingEventArgs) Handles Me.CellPainting
        Dim StrFormat As New System.Drawing.StringFormat
        Dim Rect As Rectangle = New Rectangle(e.CellBounds.X - 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height - 1)
        Dim LinearGradientBrush As New Drawing2D.LinearGradientBrush(Rect, mLinearColor1, mLinearColor2, Drawing2D.LinearGradientMode.Vertical)

        Try
            If e.RowIndex = -1 Or e.ColumnIndex = -1 Then
                e.Graphics.FillRectangle(LinearGradientBrush, Rect)
                e.Graphics.DrawRectangle(New Pen(GridColor), Rect)
                e.PaintContent(e.CellBounds)
                e.Handled = True
            End If

        Catch ex As Exception
        Finally
            Rect = Nothing
            StrFormat.Dispose()
            If LinearGradientBrush IsNot Nothing Then LinearGradientBrush.Dispose()
        End Try
End Sub