Datagridview changing the row backcolor based on value of coloum in the same grid view
-
Hi all, I have been trying to change the backcolor of specific rows depending on a condition,but when the condition is met the entire grid color changes. I already tried this and many more like this but none of them work, DataGridViewCellStyle style2 = new dataGridViewCellStyle() style2.BackColor = Color.Red; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red; There is something which I am doing wrong. Please help me to fix this.
-
Hi all, I have been trying to change the backcolor of specific rows depending on a condition,but when the condition is met the entire grid color changes. I already tried this and many more like this but none of them work, DataGridViewCellStyle style2 = new dataGridViewCellStyle() style2.BackColor = Color.Red; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red; There is something which I am doing wrong. Please help me to fix this.
dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;
dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Red;And your index can change depending on your condition. [Edit] And if you want to change multiple rows BackColor:
DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows;
foreach (DataGridViewRow row in coll)
row.DefaultCellStyle.BackColor = Color.Red;[/Edit]
When you're alone in the Dark, Fear will protect you...
modified on Sunday, November 2, 2008 8:41 AM
-
dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red;
dataGridView1.Rows[index].DefaultCellStyle.BackColor = Color.Red;And your index can change depending on your condition. [Edit] And if you want to change multiple rows BackColor:
DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows;
foreach (DataGridViewRow row in coll)
row.DefaultCellStyle.BackColor = Color.Red;[/Edit]
When you're alone in the Dark, Fear will protect you...
modified on Sunday, November 2, 2008 8:41 AM
Hi Pedram , I tried this as well it does not work, first I selected all rows which meet the specified condition This is just to show the selection, Its alternate rows here but in real scenario its any random row. int yyy = 0; foreach (DataGridViewRow rowss in dataGridView1.Rows) { if (yyy % 2 == 0) { rowss.Selected = true; } else { } yyy++; } , then I used your or each statement but still I get the same results. There is no color on the datagridview. This Datagridview is placed on a user Object and It is added on a form at the run time.Does it make any difference ? Regards Omer
-
Hi Pedram , I tried this as well it does not work, first I selected all rows which meet the specified condition This is just to show the selection, Its alternate rows here but in real scenario its any random row. int yyy = 0; foreach (DataGridViewRow rowss in dataGridView1.Rows) { if (yyy % 2 == 0) { rowss.Selected = true; } else { } yyy++; } , then I used your or each statement but still I get the same results. There is no color on the datagridview. This Datagridview is placed on a user Object and It is added on a form at the run time.Does it make any difference ? Regards Omer
But following completely worked for me:
int y = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (y % 2 == 0)
row.Selected = true;
y++;
}DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows; foreach (DataGridViewRow row in coll) row.DefaultCellStyle.BackColor = Color.Red;
But why did you act like that? It's O(n) = 2n You can:
int y = 0;
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (y % 2 == 0)
row.DefaultCellStyle.BackColor = Color.Red;
y++;
}DataGridViewSelectedRowCollection coll = dataGridView1.SelectedRows; foreach (DataGridViewRow row in coll) row.DefaultCellStyle.BackColor = Color.Red;
Member 3953856 wrote:
.Does it make any difference ?
I don't think so. You should just point to your DataGridView correctly.
When you're alone in the Dark, Fear will protect you...
-
Hi all, I have been trying to change the backcolor of specific rows depending on a condition,but when the condition is met the entire grid color changes. I already tried this and many more like this but none of them work, DataGridViewCellStyle style2 = new dataGridViewCellStyle() style2.BackColor = Color.Red; dataGridView1.RowsDefaultCellStyle.BackColor = Color.Red; There is something which I am doing wrong. Please help me to fix this.
Put your code into the cellformat event of the datagridview
-
Put your code into the cellformat event of the datagridview
Yes , putting the code on the cell formatting event of the datagridview worked, I mentioned that I was using a user control. The data grid was on the User control. If not a user control then the other stuff works fine. thanks