-
Mar 31st, 2021, 11:21 PM
#1
Thread Starter
PowerPoster
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:
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
-
Apr 1st, 2021, 02:21 AM
#2
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:
private void Form1_Load(object sender, EventArgs e)
{
DatagridviewStyling.ApplyGridStyle(dataGridView1,
RowHeadVisible: true,
AutoGenerateColumns: true,
Docking: DockStyle.None);
var table = new DataTable();
table.Columns.Add("Id", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Rows.Add(1, "Peter");
table.Rows.Add(2, "Paul");
table.Rows.Add(3, "Mary");
dataGridView1.DataSource = table;
}
private void button1_Click(object sender, EventArgs e)
{
DatagridviewStyling.ApplyGridStyle(dataGridView1,
RowHeadVisible: false,
AutoGenerateColumns: true,
Docking: DockStyle.None);
}
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.
-
Apr 4th, 2021, 11:08 PM
#3
Thread Starter
PowerPoster
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:
// This does not work Dgv_Container.Columns["ColumnName"].DefaultCellStyle.BackColor = Color.LightGreen; Dgv_Container.Rows[2].DefaultCellStyle.BackColor = Color.Yellow
Why its so,
-
Apr 4th, 2021, 11:29 PM
#4
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.
Last edited by jmcilhinney; Apr 4th, 2021 at 11:51 PM.
-
Apr 6th, 2021, 11:12 PM
#5
Thread Starter
PowerPoster
Re: Datagridview styling class
c# Code:
public partial class StyleCheckFrm : Form { public StyleCheckFrm() { InitializeComponent(); DatagridviewStyling.ApplyGridStyle(Dgv_CheckFormat); // bind Dgv_CheckFormat to list DemoUserDataModel Model = new DemoUserDataModel() ; GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Rain", Discontinue = false }); GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Coud", Discontinue = false }); GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Storm", Discontinue = false }); GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me wind", Discontinue = true }); GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me River", Discontinue = false }); GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Pond", Discontinue = false }); GridSourceModel.Add(new DemoUserDataModel { FirstName = "Make", LastName = "Me Mountain", Discontinue = false }); Dgv_CheckFormat.DataSource = GridSourceModel; } #region FormProps List<DemoUserDataModel> GridSourceModel = new List<DemoUserDataModel>(); #endregion /// <summary> /// Works Fine /// </summary> private void Dgv_CheckFormat_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { // If the user is discontinued then // make the row back color to yellow if ((Boolean) Dgv_CheckFormat.Rows[e.RowIndex].Cells["Discontinue"].Value ) { Dgv_CheckFormat.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Yellow; } } /// <summary> /// Failed, /// If the DatagridviewStyling.ApplyGridStyle(Dgv_CheckFormat) is not applied /// then the below code works ! /// </summary> private void button1_Click(object sender, EventArgs e) { Dgv_CheckFormat.AllowUserToOrderColumns = true; Dgv_CheckFormat.Columns[0].DefaultCellStyle.BackColor = Color.Green; } } public class DemoUserDataModel { public String FirstName { get; set; } public String LastName { get; set; } public Boolean Discontinue { get; set; } }
-
Apr 6th, 2021, 11:19 PM
#6
Thread Starter
PowerPoster
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|