I Have a Datagridview bound to a datatable wich hold data from a database
I made a function that checked if a date is within a correct time range
if its correct nothing happens.
else it has to change the color of the row/cell to red
I tried a lot of things but nothing works
Here is the method i created:
private void CheckFactTermijn()
{
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
DateTime FactuurDatum = Convert.ToDateTime(dataGridView1.Rows[i].Cells[2].Value.ToString());
int termijn = Convert.ToInt32(dataGridView1.Rows[i].Cells[7].Value.ToString());
DateTime finalDate = FactuurDatum.AddDays((double)termijn);
if (finalDate > DateTime.Now)
{
}
else
{
dataGridView1.Rows[i].DefaultCellStyle.BackColor = Color.Red;
}
}
}
I’m not sure when your code is running, but try setting your DataGridView’s DefaultCellStyle properties in your DGV’s DataGridView.CellFormatting Event.
The MSDN link above has an example of what you’re trying to do.
Note that you won’t be iterating through each row in your DGV (like your method does); you’d instead use the DataGridViewCellFormattingEventArgs ColumnIndex property to check which column the event fired for.