Reputation: 297
I tried to change the backcolor in a specific column when the cell value changed. I didn't find the method to do that and I don't know how to do that.
ok ----> backcolor in green.
nok ----> backcolor in red.
thank you very much for your help.
private void timer2_Tick(object sender, EventArgs e)
{
int count = 0;
foreach (DataRow dr in ds.Tables[0].Rows)
{
String StartCourse = dr[0].ToString();
string EndCourse = dr[1].ToString();
DateTime SystemTime = Convert.ToDateTime(DateTime.Now);
DateTime StartTime = Convert.ToDateTime(StartCourse);
DateTime EndTime = Convert.ToDateTime(EndCourse);
if (StartTime.TimeOfDay.Ticks <= SystemTime.TimeOfDay.Ticks && SystemTime.TimeOfDay.Ticks < EndTime.TimeOfDay.Ticks)
{
ds.Tables[0].Rows[count][5] = "ok";
}
else
{
ds.Tables[0].Rows[count][5] = "nok";
}
count++;
dataGridView1.DataSource = ds.Tables[0];
}
}
Upvotes: 0
Views: 391
Reputation: 177
I'm doing this on my own project.
foreach (DataGridViewRow row in this.dataGridView1.Rows)
{
row.DefaultCellStyle.BackColor = (row.Cells[5].Text == "ok")? Color.Green:Color.Red;
}
If that still didn't work, try delete that datagridview and add it again. Be sure to name it again as dataGridView1 and attached its corresponding events.
Upvotes: 0
Reputation: 263913
you can call this Procedure:
UPDATED
void ColorGrid()
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Cells[5].Value.ToString() == "ok")
{
row.DefaultCellStyle.BackColor = Color.Green;
}
else
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}
}
Upvotes: 1
Reputation: 6130
Try this:
foreach (DataGridViewRow row in this.DataGridView1.Rows)
{
if (row.Cells[5].Text == "ok")
{
row.DefaultCellStyle.BackColor = Color.Green;
}
else
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}
Regards
Upvotes: 0
Reputation: 70150
See the DataGridViewColumn.DefaultCellStyle property. This allows you to set a DataGridViewCellStyle
for a column. This class has a BackColor property.
See the following MSDN article for more detail:
Cell Styles in the Windows Forms DataGridView Control
Upvotes: 1