I have a class which takes a datagridview object and changes the default styling , like column header color, font and some datagridview row styling
but when i try to change the Datagridview properties at run time it does not work ?
Now during the initialization of the UI i call the above method to apply the default styling, it works fine
but while run time it does not , how to apply the fix please
but when i try to change the Datagridview properties at run time it does not work ?
c# Code:
public static class DatagridviewStyling { public static void ApplyGridStyle(DataGridView Dgv, Boolean RowHeadVisible = false, Boolean MultyCellSelect = false, Boolean UsersCanAddRows = false, Boolean AllowColumnSort = false, Boolean AutoGenerateColumns = false, DockStyle Docking = DockStyle.Fill, DataGridViewSelectionMode CellSelctionMode = DataGridViewSelectionMode.CellSelect, Color? GridDefaultBackColor = null, Color? HeaderDefaultBackColor = null, Color? HeaderDefaultForeColor = null, Font HeaderFont = null, Color? GridLineDefaultColor = null, Color? CellForeColor = null, Color? CellBackColor = null, Font CellFont = null, Color? CellSelectionColor = null, Color? CellSelectionForeColor = null) { Font DefaultHeaderFont = new Font("Microsoft Sans Serif", 12,FontStyle.Bold); Font DefaultCellFont = new Font("Microsoft Sans Serif", 11,FontStyle.Bold); Dgv.ScrollBars = ScrollBars.Both; Dgv.EnableHeadersVisualStyles = false; Dgv.AutoGenerateColumns = AutoGenerateColumns; Dgv.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.DarkSlateBlue; Dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; Dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None; Dgv.ColumnHeadersHeight = 30; Dgv.BorderStyle = BorderStyle.None; Dgv.RowHeadersVisible = RowHeadVisible; Dgv.MultiSelect = MultyCellSelect; Dgv.AllowUserToAddRows = UsersCanAddRows; Dgv.Dock = Docking; Dgv.SelectionMode = CellSelctionMode; Dgv.AllowUserToResizeColumns = true; Dgv.AllowUserToResizeRows = true; Dgv.AllowUserToOrderColumns = AllowColumnSort; Dgv.BackgroundColor = GridDefaultBackColor == null? (Color)SystemColors.ControlLightLight : (Color)GridDefaultBackColor; Dgv.RowsDefaultCellStyle.BackColor = CellBackColor == null ? Color.White : (Color)CellBackColor; Dgv.RowsDefaultCellStyle.ForeColor = CellForeColor == null? Color.FromArgb(0, 0, 204) : (Color)CellForeColor; // Color.FromArgb(70, 128, 185) Dgv.RowsDefaultCellStyle.SelectionBackColor = CellSelectionColor == null ? Color.DodgerBlue : (Color)CellSelectionColor; Dgv.RowsDefaultCellStyle.SelectionForeColor = CellSelectionForeColor == null ? Color.White : (Color)CellSelectionForeColor; Dgv.ColumnHeadersDefaultCellStyle.Font = HeaderFont == null? DefaultHeaderFont : HeaderFont; Dgv.GridColor = GridLineDefaultColor == null ? Color.FromArgb(255, 235, 230) : (Color)GridLineDefaultColor; Dgv.ColumnHeadersDefaultCellStyle.ForeColor = HeaderDefaultForeColor == null ? Color.White : (Color)HeaderDefaultForeColor; Dgv.ColumnHeadersDefaultCellStyle.BackColor = HeaderDefaultBackColor == null ? Color.SteelBlue : (Color)HeaderDefaultBackColor; Dgv.RowsDefaultCellStyle.Font = CellFont == null ? DefaultCellFont : CellFont; foreach (DataGridViewColumn item in Dgv.Columns) { item.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells; item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter; } } }
Now during the initialization of the UI i call the above method to apply the default styling, it works fine
but while run time it does not , how to apply the fix please