VB.NET 自定义DataGridView样式
vb.net吧
全部回复
仅看楼主
level 2
rahafa 楼主
类形式的,还能自动填满窗体,直接上代码
Class SurroundingClass
Public Sub SetGridViewType(ByVal dgv As DataGridView)
dgv.BackgroundColor = Color.White
dgv.Margin = New Padding(0, 0, 0, 0)
dgv.AllowUserToDeleteRows = False
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgv.BackgroundColor = Color.White
dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
'标题样式
Dim dataGridViewCellStyleTitle As DataGridViewCellStyle = New DataGridViewCellStyle()
dataGridViewCellStyleTitle.Alignment = DataGridViewContentAlignment.MiddleLeft
dataGridViewCellStyleTitle.BackColor = Color.SlateGray
dataGridViewCellStyleTitle.Font = New Font("黑体", 15.0F, FontStyle.Regular, GraphicsUnit.Point, 134)
dataGridViewCellStyleTitle.ForeColor = Color.Gold
dataGridViewCellStyleTitle.SelectionBackColor = SystemColors.Highlight
dataGridViewCellStyleTitle.SelectionForeColor = SystemColors.HighlightText
dataGridViewCellStyleTitle.WrapMode = DataGridViewTriState.[True]
dgv.ColumnHeadersDefaultCellStyle = dataGridViewCellStyleTitle
'行默认样式
Dim dataGridViewCellStyleRowsDefault As DataGridViewCellStyle = New DataGridViewCellStyle()
dataGridViewCellStyleRowsDefault.Alignment = DataGridViewContentAlignment.MiddleLeft
dataGridViewCellStyleRowsDefault.BackColor = SystemColors.Window
dataGridViewCellStyleRowsDefault.Font = New Font("黑体", 11.0F, FontStyle.Regular, GraphicsUnit.Point, 134)
dataGridViewCellStyleRowsDefault.ForeColor = SystemColors.ControlText
dataGridViewCellStyleRowsDefault.ForeColor = Color.DarkCyan
dataGridViewCellStyleRowsDefault.SelectionBackColor = SystemColors.Highlight
dataGridViewCellStyleRowsDefault.SelectionForeColor = SystemColors.HighlightText
dataGridViewCellStyleRowsDefault.WrapMode = DataGridViewTriState.[False]
dgv.RowsDefaultCellStyle = dataGridViewCellStyleRowsDefault '设置行默认样式
'奇数行样式
Dim dataGridViewCellStyleAlternatingRows As DataGridViewCellStyle = New DataGridViewCellStyle()
dataGridViewCellStyleAlternatingRows.Alignment = DataGridViewContentAlignment.MiddleLeft
dataGridViewCellStyleAlternatingRows.BackColor = Color.DarkCyan
dataGridViewCellStyleAlternatingRows.Font = New Font("黑体", 11.0F, FontStyle.Regular, GraphicsUnit.Point, 134)
dataGridViewCellStyleAlternatingRows.ForeColor = SystemColors.ControlText
dataGridViewCellStyleAlternatingRows.ForeColor = SystemColors.Window
dataGridViewCellStyleAlternatingRows.SelectionBackColor = SystemColors.Highlight
dataGridViewCellStyleAlternatingRows.SelectionForeColor = SystemColors.HighlightText
dataGridViewCellStyleAlternatingRows.WrapMode = DataGridViewTriState.[False]
dgv.AlternatingRowsDefaultCellStyle = dataGridViewCellStyleAlternatingRows '设置奇数行样式
dgv.Dock = DockStyle.Fill
dgv.EnableHeadersVisualStyles = False
dgv.GridColor = Color.DeepSkyBlue
dgv.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single
dgv.RowHeadersWidth = 24
dgv.RowTemplate.Height = 33
End Sub
End Class
调用方法
Dim sc As New SurroundingClass
sc.SetGridViewType(DataGridView1)
在来张效果图
大家可以根据自己喜欢的风格调整数据。
2018年09月19日 01点09分 1
level 6
不错,用了!谢谢
2018年10月15日 15点10分 2
level 3
厉害
2018年10月19日 04点10分 3
level 13
[大拇指]
2019年01月21日 12点01分 4
level 1
楼为; 能给个这个程序吗
2019年06月22日 09点06分 5
level 12
赞一个
2019年07月08日 01点07分 6
level 1
楼主,id这一列是怎么添加的?
2020年03月10日 08点03分 7
这是连接了access
2020年12月26日 09点12分
level 5
厉害厉害,原来支持自定义样式
2020年12月26日 09点12分 8
1