Results 1 to 6 of 6

Thread: Datagridview styling class

  1. #1

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Datagridview styling class

    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
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Datagridview styling class

    The code you posted works perfectly fine, as far as I can tell. I just ran this code and it did exactly as expected:
    vb.net Code:
    1. private void Form1_Load(object sender, EventArgs e)
    2. {
    3.     DatagridviewStyling.ApplyGridStyle(dataGridView1,
    4.                                        RowHeadVisible: true,
    5.                                        AutoGenerateColumns: true,
    6.                                        Docking: DockStyle.None);
    7.  
    8.     var table = new DataTable();
    9.  
    10.     table.Columns.Add("Id", typeof(int));
    11.     table.Columns.Add("Name", typeof(string));
    12.  
    13.     table.Rows.Add(1, "Peter");
    14.     table.Rows.Add(2, "Paul");
    15.     table.Rows.Add(3, "Mary");
    16.  
    17.     dataGridView1.DataSource = table;
    18. }
    19.  
    20. private void button1_Click(object sender, EventArgs e)
    21. {
    22.     DatagridviewStyling.ApplyGridStyle(dataGridView1,
    23.                                        RowHeadVisible: false,
    24.                                        AutoGenerateColumns: true,
    25.                                        Docking: DockStyle.None);
    26. }
    I ran the project and the grid appeared with row headers and then I clicked the Button and the row headers were removed. I switched the values around and ran it again and, as expected, the grid was displayed without row headers and they appeared when I clicked the Button.

    Perhaps you should provide the FULL and CLEAR explanation of the problem that you should have done in the first place, i.e. show us the code you're using and explain exactly what behaviour you expect and exactly how the actual behaviour differs from that. I suspect that what's happening is that you are setting a bunch of properties to begin with and then later you try to change some properties and ignore the fact that any you don't specify will be set to the default values specified in the method signature. For example, in my code above, I had to specify DockStyle.None in the Button's Click event handler even though I wasn't changing that property, because it would have been changed to Fill if I had not provided a value. Of course, if you had provided the information that you should have when you created this thread then we would not have to speculate.

  3. #3

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview styling class

    Sir at first sorry for the late reply and partial posting of the issue

    the actual issue is
    once the datagridview styling is applied at the form initialize, and then if try say for example
    c# Code:
    1. // This does not work
    2. Dgv_Container.Columns["ColumnName"].DefaultCellStyle.BackColor = Color.LightGreen;
    3. Dgv_Container.Rows[2].DefaultCellStyle.BackColor = Color.Yellow
    Why its so,
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Datagridview styling class

    Never just say that something "doesn't work". All code works because all code does exactly what it does. You need to explain EXACTLY what you expect to happen and exactly what does happen. Don't expect us to work out what you're trying to achieve from code that doesn't do it. The issue is ALWAYS that you have not written the correct code to achieve your aim, but we can't tell you what the correct code would be if we don't know what that aim is. Tell us exactly what you're trying to achieve, show us the exact code you're using to try to achieve it and tell us exactly what happens.

  5. #5

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview styling class

    c# Code:
    1. public partial class StyleCheckFrm : Form
    2.     {
    3.         public StyleCheckFrm()
    4.         {
    5.             InitializeComponent();
    6.  
    7.  
    8.             DatagridviewStyling.ApplyGridStyle(Dgv_CheckFormat);
    9.  
    10.  
    11.             // bind Dgv_CheckFormat to list
    12.             DemoUserDataModel Model = new DemoUserDataModel() ;
    13.  
    14.  
    15.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Rain", Discontinue = false });
    16.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Coud", Discontinue = false });
    17.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Storm", Discontinue = false });
    18.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me wind", Discontinue = true });
    19.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me River", Discontinue = false });
    20.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Pond", Discontinue = false });
    21.             GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Mountain", Discontinue = false });
    22.  
    23.  
    24.             Dgv_CheckFormat.DataSource = GridSourceModel;
    25.  
    26.  
    27.         }
    28.  
    29.  
    30.         #region FormProps
    31.         List<DemoUserDataModel> GridSourceModel = new List<DemoUserDataModel>();
    32.         #endregion
    33.        
    34.  
    35.  
    36.         /// <summary>
    37.         /// Works Fine        
    38.         /// </summary>        
    39.         private void Dgv_CheckFormat_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    40.         {
    41.             // If the user is discontinued then
    42.             // make the row back color to yellow
    43.  
    44.  
    45.             if ((Boolean) Dgv_CheckFormat.Rows[e.RowIndex].Cells["Discontinue"].Value )
    46.             {
    47.                 Dgv_CheckFormat.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow;              
    48.             }
    49.  
    50.  
    51.         }
    52.  
    53.  
    54.         /// <summary>
    55.         /// Failed,
    56.         /// If the  DatagridviewStyling.ApplyGridStyle(Dgv_CheckFormat) is not applied
    57.         /// then the below code works !        
    58.         /// </summary>
    59.         private void button1_Click(object sender, EventArgs e)
    60.         {
    61.             Dgv_CheckFormat.AllowUserToOrderColumns = true;
    62.             Dgv_CheckFormat.Columns[0].DefaultCellStyle.BackColor = Color.Green;
    63.         }        
    64.  
    65.  
    66.     }
    67.  
    68.  
    69.     public class DemoUserDataModel
    70.     {
    71.        public String FirstName { get; set; }
    72.        public  String LastName { get; set; }
    73.        public  Boolean Discontinue { get; set; }
    74.     }
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

  6. #6

    Thread Starter
    PowerPoster make me rain's Avatar
    Join Date
    Sep 2008
    Location
    india/Hubli
    Posts
    2,208

    Re: Datagridview styling class

    Here specifically Column property changes does not work,
    I done tons of googling for a whole day,
    Here the code does execute but no effect on the intended property change
    The averted nuclear war
    My notes:

    PrOtect your PC. MSDN Functions .OOP LINUX forum
    .LINQ LINQ videous
    If some one helps you please rate them with out fail , forum doesn't expects any thing other than this

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width