Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Wednesday, July 10, 2013 8:47 PM
Hi all,
I can change either the column or the row background color of a datagridview control with this code (i is part of a for loop):
myDataGridView.Columns[i].DefaultCellStyle.BackColor = Color.Aquamarine;
myDataGridView.Rows[i].DefaultCellStyle.BackColor = Color.Cornsilk;
I want to set the same row background color for records that have the same "day" in a date field. This worked. But then I wanted to have all of column 3 with Aquamarine background. This does not work after I set the row background color. It works if I don't set the row background color though.
What Am I missing?
Thanks
All replies (1)
Thursday, July 11, 2013 1:58 AM âś…Answered | 3 votes
Hi,
you have to apply the style to the cells that are in both styles explicitely. Else row takes precedence over columns...
public partial class Form1 : Form
{
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
public Form1()
{
InitializeComponent();
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Location = new System.Drawing.Point(29, 64);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(513, 268);
this.button1.Location = new System.Drawing.Point(536, 406);
this.button1.Name = "button1";
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Location = new System.Drawing.Point(375, 396);
this.button2.Name = "button2";
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(749, 482);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.dataGridView1);
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("c1");
dt.Columns.Add("c2");
for (int i = 0; i < 5; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i.ToString();
dr[1] = i.ToString();
dt.Rows.Add(dr);
}
this.dataGridView1.DataSource = dt.DefaultView;
}
private void button2_Click(object sender, EventArgs e)
{
this.dataGridView1.Rows[1].DefaultCellStyle.BackColor = Color.Cornsilk;
this.dataGridView1.Rows[1].Cells[1].Style.ApplyStyle(this.dataGridView1.Rows[1].DefaultCellStyle);
}
private void button1_Click(object sender, EventArgs e)
{
this.dataGridView1.Columns[1].DefaultCellStyle.BackColor = Color.Aquamarine;
this.dataGridView1.Rows[1].Cells[1].Style.ApplyStyle(this.dataGridView1.Columns[1].DefaultCellStyle);
}
}
Regards,
Thorsten