Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15442

5.0 Datagridview styling class

$
0
0
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 ?

c# Code:
  1. public static  class DatagridviewStyling
  2.     {
  3.  
  4.  
  5.  
  6.  
  7.         public static void ApplyGridStyle(DataGridView Dgv,
  8.                                            Boolean RowHeadVisible = false,
  9.                                            Boolean MultyCellSelect = false,
  10.                                            Boolean UsersCanAddRows = false,
  11.                                            Boolean AllowColumnSort = false,
  12.                                            Boolean AutoGenerateColumns = false,
  13.                                            DockStyle Docking = DockStyle.Fill,
  14.                                            DataGridViewSelectionMode CellSelctionMode = DataGridViewSelectionMode.CellSelect,                
  15.                                            Color? GridDefaultBackColor = null,
  16.                                            Color? HeaderDefaultBackColor = null,
  17.                                            Color? HeaderDefaultForeColor = null,
  18.                                            Font HeaderFont = null,
  19.                                            Color? GridLineDefaultColor = null,
  20.                                            Color? CellForeColor = null,
  21.                                            Color? CellBackColor = null,
  22.                                            Font CellFont = null,
  23.                                            Color? CellSelectionColor = null,
  24.                                            Color? CellSelectionForeColor = null)
  25.                                            
  26.         {
  27.             Font DefaultHeaderFont = new Font("Microsoft Sans Serif", 12,FontStyle.Bold);
  28.             Font DefaultCellFont = new Font("Microsoft Sans Serif", 11,FontStyle.Bold);
  29.  
  30.  
  31.             Dgv.ScrollBars = ScrollBars.Both;
  32.             Dgv.EnableHeadersVisualStyles = false;
  33.             Dgv.AutoGenerateColumns = AutoGenerateColumns;
  34.             Dgv.ColumnHeadersDefaultCellStyle.SelectionBackColor = Color.DarkSlateBlue;
  35.            
  36.             Dgv.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
  37.             Dgv.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
  38.             Dgv.ColumnHeadersHeight = 30;
  39.             Dgv.BorderStyle = BorderStyle.None;
  40.             Dgv.RowHeadersVisible = RowHeadVisible;
  41.             Dgv.MultiSelect = MultyCellSelect;
  42.             Dgv.AllowUserToAddRows = UsersCanAddRows;            
  43.             Dgv.Dock = Docking;
  44.             Dgv.SelectionMode = CellSelctionMode;
  45.             Dgv.AllowUserToResizeColumns = true;
  46.             Dgv.AllowUserToResizeRows = true;
  47.             Dgv.AllowUserToOrderColumns = AllowColumnSort;
  48.  
  49.  
  50.             Dgv.BackgroundColor =
  51.                 GridDefaultBackColor == null? (Color)SystemColors.ControlLightLight : (Color)GridDefaultBackColor;
  52.  
  53.  
  54.             Dgv.RowsDefaultCellStyle.BackColor = CellBackColor == null ? Color.White : (Color)CellBackColor;
  55.             Dgv.RowsDefaultCellStyle.ForeColor = CellForeColor == null? Color.FromArgb(0, 0, 204) : (Color)CellForeColor;
  56.             // Color.FromArgb(70, 128, 185)
  57.             Dgv.RowsDefaultCellStyle.SelectionBackColor =
  58.                 CellSelectionColor == null ? Color.DodgerBlue : (Color)CellSelectionColor;
  59.  
  60.  
  61.             Dgv.RowsDefaultCellStyle.SelectionForeColor = CellSelectionForeColor == null ? Color.White : (Color)CellSelectionForeColor;
  62.  
  63.  
  64.             Dgv.ColumnHeadersDefaultCellStyle.Font = HeaderFont == null? DefaultHeaderFont : HeaderFont;
  65.             Dgv.GridColor = GridLineDefaultColor == null ? Color.FromArgb(255, 235, 230) : (Color)GridLineDefaultColor;
  66.  
  67.  
  68.             Dgv.ColumnHeadersDefaultCellStyle.ForeColor =
  69.                 HeaderDefaultForeColor == null ? Color.White : (Color)HeaderDefaultForeColor;
  70.  
  71.  
  72.             Dgv.ColumnHeadersDefaultCellStyle.BackColor =
  73.                 HeaderDefaultBackColor == null ? Color.SteelBlue : (Color)HeaderDefaultBackColor;
  74.  
  75.  
  76.             Dgv.RowsDefaultCellStyle.Font = CellFont == null ? DefaultCellFont : CellFont;
  77.  
  78.  
  79.             foreach (DataGridViewColumn  item in Dgv.Columns)
  80.             {
  81.                 item.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
  82.                 item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
  83.             }
  84.         }
  85.        
  86.     }

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

Viewing all articles
Browse latest Browse all 15442

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>