类形式的,还能自动填满窗体,直接上代码
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)
在来张效果图
大家可以根据自己喜欢的风格调整数据。
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)
在来张效果图
大家可以根据自己喜欢的风格调整数据。