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

一、关于起因

最近非常频繁的使用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 String
100     Public Property ColumnAlignment() As String
101         Get
102             Return mColumnAlignment
103         End Get
104         Set(ByVal value As String)
105             mColumnAlignment = value
106             Dim mColCount As Integer = mGrid.ColumnCount
107             Dim mAlignments() As String = value.Split(";")
108             For i = 0 To mAlignments.Length - 1
109                 If i + 1 > mColCount Then Exit For
110                 Select Case mAlignments(i)
111                     Case Is = "L"
112                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
113                     Case Is = "R"
114                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
115                     Case Is = "M"
116                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
117                     Case Else
118                         mGrid.Columns(i).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
119                 End Select
120             Next
121         End Set
122     End Property
123
124     Private mHeaderFont As Font
125     Public Property HeaderFont() As Font
126         Get
127             Return mHeaderFont
128         End Get
129         Set(ByVal value As Font)
130             mHeaderFont = value
131             mGrid.ColumnHeadersDefaultCellStyle.Font = value
132         End Set
133     End Property
134
135     Private mHeaderHeight As Integer
136     Public Property HeaderHeight() As Integer
137         Get
138             Return mHeaderHeight
139         End Get
140         Set(ByVal value As Integer)
141             mHeaderHeight = value
142             mGrid.ColumnHeadersHeight = value
143         End Set
144     End Property
145
146     Private mHeaderForeColor As Color
147     Public Property HeaderForeColor() As Color
148         Get
149             Return mHeaderForeColor
150         End Get
151         Set(ByVal value As Color)
152             mHeaderForeColor = value
153             mGrid.ColumnHeadersDefaultCellStyle.ForeColor = value
154         End Set
155     End Property
156
157     Private mRowHeaderWidth As Integer
158     Public Property RowHeaderWidth() As Integer
159         Get
160             Return mRowHeaderWidth
161         End Get
162         Set(ByVal value As Integer)
163             mRowHeaderWidth = value
164             mGrid.RowHeadersWidth = value
165         End Set
166     End Property
167
168     Private mRowHeight As Integer
169     Public Property RowHeight() As Integer
170         Get
171             Return mRowHeight
172         End Get
173         Set(ByVal value As Integer)
174             mRowHeight = value
175             For i = 0 To mGrid.Rows.Count - 1
176                 mGrid.Rows(i).Height = value
177             Next
178         End Set
179     End Property
180
181     Private mRowFont As Font
182     Public Property RowFont() As Font
183         Get
184             Return mRowFont
185         End Get
186         Set(ByVal value As Font)
187             mRowFont = value
188             mGrid.RowsDefaultCellStyle.Font = value
189         End Set
190     End Property
191
192     Private mBackColor As Color
193     Public Property BackColor() As Color
194         Get
195             Return mBackColor
196         End Get
197         Set(ByVal value As Color)
198             mBackColor = value
199             mGrid.BackgroundColor = value
200         End Set
201     End Property
202
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 = TargetGrid
208         Me.BackColor = BackColor
209
210         Me.HeaderFont = HeaderFont
211         Me.HeaderHeight = HeaderHeight
212         Me.HeaderBackColor = HeaderBackColor
213         Me.HeaderForeColor = HeaderForeColor
214         Me.RowHeaderWidth = RowHeaderWidth
215
216         Me.RowFont = RowFont
217         Me.RowHeight = RowHeight
218         Me.OddRowBackColor = OddRowBackColor
219         Me.EvenRowBackColor = EvenRowBackColor
220
221         Me.GridColor = GridColor
222         Me.ColumnWidth = ColumnWidth
223         Me.ColumnAlignment = ColumnAlignment
224     End Sub
225
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 Sub
230
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 = BackColor
236
237         Me.HeaderFont = HeaderFont
238         Me.HeaderHeight = HeaderHeight
239         Me.HeaderBackColor = HeaderBackColor
240         Me.HeaderForeColor = HeaderForeColor
241         Me.RowHeaderWidth = RowHeaderWidth
242
243         Me.RowFont = RowFont
244         Me.RowHeight = RowHeight
245         Me.OddRowBackColor = OddRowBackColor
246         Me.EvenRowBackColor = EvenRowBackColor
247
248         Me.GridColor = GridColor
249         Me.ColumnWidth = ColumnWidth
250         Me.ColumnAlignment = ColumnAlignment
251     End Sub
252 End Class
时间: 2024-10-28 23:20:08

一个Winform下DataGridView控件外观的自定义类的相关文章

Winform 中DataGridView控件添加行标题

有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) 1 /// <summary> 2 /// 行状态更改时发生 3 /// </summary> 4 /// <param name="sender"></param> 5 /// <param name="e"></param> 6 pri

.NET WinForm下StatusStrip控件如何设置分隔线及部分子控件右对齐

ssInfo.LayoutStyle = ToolStripLayoutStyle.StackWithOverflow;//StatusStrip 控件 tsslUpdate.Alignment = ToolStripItemAlignment.Right;//StatusStrip 中的 ToolStripStatus 子控件

c# WinForm开发 DataGridView控件的各种操作总结(单元格操作,属性设置)

一.单元格内容的操作 *****// 取得当前单元格内容 Console.WriteLine(DataGridView1.CurrentCell.Value); // 取得当前单元格的列 Index       Console.WriteLine(DataGridView1.CurrentCell.ColumnIndex); // 取得当前单元格的行 Index       Console.WriteLine(DataGridView1.CurrentCell.RowIndex); ******

C#WinForm的DataGridView控件显示行号

public void ShowIndex(DataGridView dgv)        {                       for (int i = 0; i < dgv.Rows.Count; i++)            {                               dgv.Rows[i].HeaderCell.Value = (i+1).ToString();  //HeaderCell获取或设置行的标头单元格                     

Winform下pictruebox控件加载网络图片

1 string s = "http://www.google.com/intl/zh-CN_ALL/images/logo.gif"; 2 System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(s); 3 System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)req.GetResponse

DataGridView控件-[引用]

DataGridView控件 DataGridView是用于Windows Froms 2.0的新网格控件.它可以取代先前版本中DataGrid控件,它易于使用并高度可定制,支持很多我们的用户需要的特性. 关于本文档: 本文档不准备面面俱到地介绍DataGridView,而是着眼于深入地介绍一些技术点的高级特性. 本文档按逻辑分为5个章节,首先是结构和特性的概览,其次是内置的列/单元格类型的介绍,再次是数据操作相关的内容,然后是主要特性的综述,最后是最佳实践. 大部分章节含有一个"Q &

Winform开发常用控件之DataGridView的简单数据绑定——自动绑定

DataGridView控件可谓是Winform开发的重点控件,对于数据的呈现和操作非常方便,DataGridView可谓是既简单又复杂.简单在于其已经集成了很多方法,复杂在于可以使用其实现复杂的数据呈现和操作. 本文是入门级培训,先介绍DataGridView的简单应用,复杂的应用在后续的博文中会一一呈上. DataGridView主要是呈现数据和数据操作的,那自然离不开数据. 首先是数据绑定,DataGridView的数据源可以是DataSet.DataTable或Ilist等,至于Data

C#WinForm 显示选中的行是第几行,datagridview控件

1 UI 2 keyCode 1 private void button3_Click_1(object sender, EventArgs e) 2 { 3 //不加1的话,选取第一行的时候提示0,第二行提示1,所以我加了一个1,方便看 4 //为什么要加索引0,因为我有可能选取多行,这个时候该如何显示我选取的是第几行呢? 5 //答案是按照你选取的顺序,输出你最后一个选中的是 第几行 6 //例如我,选中 1 2 3行,3行是我最后一个选中的,所以输出3 7 // 选中 10 2 1行,1行

使用两个 Windows 窗体 DataGridView 控件创建一个主/从窗体

使用 DataGridView 控件的一种最常见方案是"主/详细信息"窗体,这样的窗体可显示两个数据库表之间的父/子关系.如果选择主表中的行,将导致以相应的子数据来更新详细信息表. 主/详细信息窗体很容易实现,这需要使用 DataGridView 控件和 BindingSource 组件之间的交互.在本演练中,将使用两个 DataGridView 控件和两个 BindingSource 组件来生成窗体.窗体将显示 Northwind SQL Server 示例数据库中的两个相关表:Cu