首页 > 代码库 > 一个Winform下DataGridView控件外观的自定义类

一个Winform下DataGridView控件外观的自定义类

<style></style>
一、关于起因

最近非常频繁的使用DataGridView控件,VS提供的Winform下DataGridView的默认样式非常难看。如果做美化处理的话,需要调整的地方非常多,且该控件的很多设置属性非常的晦涩,不是很人性化。每次进行设置都煞费脑筋,并且需要设置的DataGridView控件又比较多,如果手工一个一个调整非常麻烦。如果在每个窗体里逐个一行一行的用代码来设置,会造成代码很凌乱、琐碎,因此我对DataGridView格式设置的常用项封装到一个Helper类,通过这个帮助类可以一步到位完成设置。

对DataGridView控件,我需要完成的一些基本的格式控制,包括以下一些内容:

1、标题行背景色、前景色、字体设置、平面化样式、行高

2、标题列宽度,平面化样式

3、偶数行背景色、前景色

4、奇数行背景色、前景色

5、数据行高度、字体

6、列宽度,对齐方式

7、禁止编辑、添加、删除、调整行高、列宽、排序

8、去除控件外边框

最终处理过后的风格类似如下图所示

技术分享

二、CGridHelper类模块的调用

最终我所希望的实现格式化DataGridView控件的形式是通过一行代码来实现,类似如下所示:

1         Dim mGridHelper As New CGridHelper(GridVersion, Color.FromArgb(250, 250, 250),2             New Font("微软雅黑", 10), 30, Color.FromArgb(159, 210, 235), Color.FromArgb(0, 0, 0), 24,3             New Font("微软雅黑", 9), 30, Color.FromArgb(250, 250, 250), Color.FromArgb(225, 225, 225), Color.FromArgb(0, 0, 0),4             "0;80;70;595", "M;M;M;L")

其中列宽的控制通过一个字符串来设置,用分号来分隔数字,各数字依次设置对应列的宽度;列的对齐方式也类似,不过左、中、右分别用字母L/M/R表示。

三、CGridHelper类模块代码

接下来直接上CGridHelper类模块的代码了,代码还比较粗糙,但是满足我自己的需求就够了。

  1 Public Class CGridHelper  2     Private mGrid As DataGridView  3     Public Property Grid() As DataGridView  4         Get  5             Return mGrid  6         End Get  7         Set(ByVal value As DataGridView)  8             mGrid = value  9             mGrid.BorderStyle = BorderStyle.None 10             mGrid.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single 11             mGrid.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.False 12             mGrid.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing 13             mGrid.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single 14             mGrid.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing 15             mGrid.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.None 16             mGrid.DefaultCellStyle.WrapMode = DataGridViewTriState.True 17             mGrid.EnableHeadersVisualStyles = False 18             mGrid.AllowUserToResizeRows = False 19             mGrid.SelectionMode = DataGridViewSelectionMode.FullRowSelect 20             mGrid.ReadOnly = True 21             mGrid.AllowUserToAddRows = False 22             mGrid.AllowUserToDeleteRows = False 23             For i = 0 To mGrid.Columns.Count - 1 24                 mGrid.Columns(i).SortMode = DataGridViewColumnSortMode.NotSortable 25             Next 26         End Set 27     End Property 28  29     Private mOddRowBackColor As Color 30     Public Property OddRowBackColor() As Color 31         Get 32             Return mOddRowBackColor 33         End Get 34         Set(ByVal value As Color) 35             mOddRowBackColor = value 36             If mGrid.Rows.Count = 0 Then Exit Property 37             For i = 0 To mGrid.Rows.Count - 1 Step 2 38                 mGrid.Rows(i).DefaultCellStyle.BackColor = value 39             Next 40         End Set 41     End Property 42  43     Private mHeaderBackColor As Color 44     Public Property HeaderBackColor() As Color 45         Get 46             Return mHeaderBackColor 47         End Get 48         Set(ByVal value As Color) 49             mHeaderBackColor = value 50             mGrid.ColumnHeadersDefaultCellStyle.BackColor = value 51         End Set 52     End Property 53  54     Private mEvenRowBackColor As Color 55     Public Property EvenRowBackColor() As Color 56         Get 57             Return mEvenRowBackColor 58         End Get 59         Set(ByVal value As Color) 60             mEvenRowBackColor = value 61             If mGrid.Rows.Count = 0 Then Exit Property 62             For i = 1 To mGrid.Rows.Count - 1 Step 2 63                 mGrid.Rows(i).DefaultCellStyle.BackColor = value 64             Next 65         End Set 66     End Property 67  68     Private mGridColor As Color 69     Public Property GridColor() As Color 70         Get 71             Return mGridColor 72         End Get 73         Set(ByVal value As Color) 74             mGridColor = value 75             mGrid.GridColor = value 76         End Set 77     End Property 78  79     Private mColumnWidth As String 80     Public Property ColumnWidth() As String 81         Get 82             Return mColumnWidth 83         End Get 84         Set(ByVal value As String) 85             mColumnWidth = value 86             Dim mColCount As Integer = mGrid.ColumnCount 87             Dim mWidths() As String = value.Split(";") 88             For i = 0 To mWidths.Length - 1 89                 If i + 1 > mColCount Then Exit For 90                 If mWidths(i) <= 0 Then 91                     mGrid.Columns(i).Visible = False 92                 Else 93                     mGrid.Columns(i).Width = mWidths(i) 94                 End If 95             Next 96         End Set 97     End Property 98  99     Private mColumnAlignment As String100     Public Property ColumnAlignment() As String101         Get102             Return mColumnAlignment103         End Get104         Set(ByVal value As String)105             mColumnAlignment = value106             Dim mColCount As Integer = mGrid.ColumnCount107             Dim mAlignments() As String = value.Split(";")108             For i = 0 To mAlignments.Length - 1109                 If i + 1 > mColCount Then Exit For110                 Select Case mAlignments(i)111                     Case Is = "L"112                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft113                     Case Is = "R"114                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight115                     Case Is = "M"116                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter117                     Case Else118                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft119                 End Select120             Next121         End Set122     End Property123 124     Private mHeaderFont As Font125     Public Property HeaderFont() As Font126         Get127             Return mHeaderFont128         End Get129         Set(ByVal value As Font)130             mHeaderFont = value131             mGrid.ColumnHeadersDefaultCellStyle.Font = value132         End Set133     End Property134 135     Private mHeaderHeight As Integer136     Public Property HeaderHeight() As Integer137         Get138             Return mHeaderHeight139         End Get140         Set(ByVal value As Integer)141             mHeaderHeight = value142             mGrid.ColumnHeadersHeight = value143         End Set144     End Property145 146     Private mHeaderForeColor As Color147     Public Property HeaderForeColor() As Color148         Get149             Return mHeaderForeColor150         End Get151         Set(ByVal value As Color)152             mHeaderForeColor = value153             mGrid.ColumnHeadersDefaultCellStyle.ForeColor = value154         End Set155     End Property156 157     Private mRowHeaderWidth As Integer158     Public Property RowHeaderWidth() As Integer159         Get160             Return mRowHeaderWidth161         End Get162         Set(ByVal value As Integer)163             mRowHeaderWidth = value164             mGrid.RowHeadersWidth = value165         End Set166     End Property167 168     Private mRowHeight As Integer169     Public Property RowHeight() As Integer170         Get171             Return mRowHeight172         End Get173         Set(ByVal value As Integer)174             mRowHeight = value175             For i = 0 To mGrid.Rows.Count - 1176                 mGrid.Rows(i).Height = value177             Next178         End Set179     End Property180 181     Private mRowFont As Font182     Public Property RowFont() As Font183         Get184             Return mRowFont185         End Get186         Set(ByVal value As Font)187             mRowFont = value188             mGrid.RowsDefaultCellStyle.Font = value189         End Set190     End Property191 192     Private mBackColor As Color193     Public Property BackColor() As Color194         Get195             Return mBackColor196         End Get197         Set(ByVal value As Color)198             mBackColor = value199             mGrid.BackgroundColor = value200         End Set201     End Property202 203     Public Sub New(TargetGrid As DataGridView, BackColor As Color, HeaderFont As Font, HeaderHeight As Integer,204             HeaderBackColor As Color, HeaderForeColor As Color, RowHeaderWidth As Integer,205             RowFont As Font, RowHeight As Integer, OddRowBackColor As Color, EvenRowBackColor As Color,206             GridColor As Color, ColumnWidth As String, ColumnAlignment As String)207         Me.Grid = TargetGrid208         Me.BackColor = BackColor209 210         Me.HeaderFont = HeaderFont211         Me.HeaderHeight = HeaderHeight212         Me.HeaderBackColor = HeaderBackColor213         Me.HeaderForeColor = HeaderForeColor214         Me.RowHeaderWidth = RowHeaderWidth215 216         Me.RowFont = RowFont217         Me.RowHeight = RowHeight218         Me.OddRowBackColor = OddRowBackColor219         Me.EvenRowBackColor = EvenRowBackColor220 221         Me.GridColor = GridColor222         Me.ColumnWidth = ColumnWidth223         Me.ColumnAlignment = ColumnAlignment224     End Sub225 226     Public Sub ReSetStyle()227         ReSetStyle(Me.BackColor, Me.HeaderFont, Me.HeaderHeight, Me.HeaderBackColor, Me.HeaderForeColor, Me.RowHeaderWidth,228                  Me.RowFont, Me.RowHeight, Me.OddRowBackColor, Me.EvenRowBackColor, Me.GridColor, Me.ColumnWidth, Me.ColumnAlignment)229     End Sub230 231     Private Sub ReSetStyle(BackColor As Color, HeaderFont As Font, HeaderHeight As Integer,232             HeaderBackColor As Color, HeaderForeColor As Color, RowHeaderWidth As Integer,233             RowFont As Font, RowHeight As Integer, OddRowBackColor As Color, EvenRowBackColor As Color,234             GridColor As Color, ColumnWidth As String, ColumnAlignment As String)235         Me.BackColor = BackColor236 237         Me.HeaderFont = HeaderFont238         Me.HeaderHeight = HeaderHeight239         Me.HeaderBackColor = HeaderBackColor240         Me.HeaderForeColor = HeaderForeColor241         Me.RowHeaderWidth = RowHeaderWidth242 243         Me.RowFont = RowFont244         Me.RowHeight = RowHeight245         Me.OddRowBackColor = OddRowBackColor246         Me.EvenRowBackColor = EvenRowBackColor247 248         Me.GridColor = GridColor249         Me.ColumnWidth = ColumnWidth250         Me.ColumnAlignment = ColumnAlignment251     End Sub252 End Class

 

一个Winform下DataGridView控件外观的自定义类