I'm Trying make DataGridViewTheme class
to change the style of the DatagridView theme
but the row size results are not the same in datagridview2 using the DataGridViewTheme class compared to datagridview1 Even though the code used is the same
Is there something wrong with my code, please guide me
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DataGridView1.AllowUserToResizeRows = False
DataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
DataGridView1.BorderStyle = BorderStyle.None
DataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None
DataGridView1.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.ColumnHeadersDefaultCellStyle.Font = New Font("Segoe UI Semibold", 10.0F, System.Drawing.FontStyle.Bold)
DataGridView1.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
DataGridView1.ColumnHeadersHeight = 40
DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
DataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
DataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.False
DataGridView1.DefaultCellStyle.Font = New Font("Segoe UI", 10.0F)
DataGridView1.EnableHeadersVisualStyles = False
DataGridView1.RowHeadersVisible = False
DataGridView1.RowHeadersWidth = 40
DataGridView1.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
DataGridView1.RowTemplate.Height = 40
DataGridView1.RowTemplate.Resizable = DataGridViewTriState.False
DataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect
For i As Integer = 0 To 3
DataGridView1.Rows.Add(New Object() {"Column 1", "Column 2", "Column 3"})
Next i
'set theme
DataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(210, 232, 254)
DataGridView1.AlternatingRowsDefaultCellStyle.ForeColor = Color.Black
DataGridView1.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.LightSkyBlue
DataGridView1.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Black
DataGridView1.BackgroundColor = Color.FromArgb(248, 251, 254)
DataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(30, 144, 254)
DataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
DataGridView1.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(30, 144, 254)
DataGridView1.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White
DataGridView1.DefaultCellStyle.BackColor = Color.FromArgb(248, 251, 254)
DataGridView1.DefaultCellStyle.ForeColor = Color.Black
DataGridView1.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue
DataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
For i As Integer = 0 To 3
DataGridView2.Rows.Add(New Object() {"Column 1", "Column 2", "Column 3"})
Next i
Dim lightTheme As New DataGridViewTheme(DataGridView2, DataGridViewTheme.Theme.Light)
End Sub
End Class
code in class DataGridViewTheme
Public Class DataGridViewTheme
Public Enum Theme
Light
End Enum
Public Sub New(ByVal dgv As DataGridView, ByVal Theme As Theme)
dgv.AllowUserToResizeRows = False
dgv.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
dgv.BorderStyle = BorderStyle.None
dgv.CellBorderStyle = DataGridViewCellBorderStyle.None
dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None
dgv.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
dgv.ColumnHeadersDefaultCellStyle.Font = New Font("Segoe UI Semibold", 10.0F, System.Drawing.FontStyle.Bold)
dgv.ColumnHeadersDefaultCellStyle.WrapMode = DataGridViewTriState.True
dgv.ColumnHeadersHeight = 40
dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing
dgv.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft
dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.False
dgv.DefaultCellStyle.Font = New Font("Segoe UI", 10.0F)
dgv.EnableHeadersVisualStyles = False
dgv.RowHeadersVisible = False
dgv.RowHeadersWidth = 40
dgv.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing
dgv.RowTemplate.Height = 40
dgv.RowTemplate.Resizable = DataGridViewTriState.False
dgv.SelectionMode = DataGridViewSelectionMode.FullRowSelect
Select Case Theme
Case DataGridViewTheme.Theme.Light
Light(dgv)
Case Else
End Select
End Sub
Private Sub Light(ByVal dgv As DataGridView)
dgv.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(210, 232, 254)
dgv.AlternatingRowsDefaultCellStyle.ForeColor = Color.Black
dgv.AlternatingRowsDefaultCellStyle.SelectionBackColor = Color.LightSkyBlue
dgv.AlternatingRowsDefaultCellStyle.SelectionForeColor = Color.Black
dgv.BackgroundColor = Color.FromArgb(248, 251, 254)
dgv.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(30, 144, 254)
dgv.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
dgv.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.FromArgb(30, 144, 254)
dgv.ColumnHeadersDefaultCellStyle.SelectionForeColor = Color.White
dgv.DefaultCellStyle.BackColor = Color.FromArgb(248, 251, 254)
dgv.DefaultCellStyle.ForeColor = Color.Black
dgv.DefaultCellStyle.SelectionBackColor = Color.LightSkyBlue
dgv.DefaultCellStyle.SelectionForeColor = Color.Black
dgv.Refresh()
End Sub
End Class
Desired result in datagridview2 like my green color marking in datagridview1
In the case of the first grid, you setup the theme and then you add the data. In the case of the second grid, you do the opposite. You are seting the height of the row template, not of the rows. That change will only affect rows created later, based on the changed template. It doesn't affect any existing rows, which have already been created based on the old template.