Hi ernestpauld,
Please refer below updated code.
Code
C#
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] {
new DataColumn("Id"),
new DataColumn("accountstatus"),
new DataColumn("FromDate")});
dt.Rows.Add(1, "INACTIVE", "10/12/2023");
dt.Rows.Add(2, "INACTIVE", "11/12/2023");
dt.Rows.Add(3, "ACTIVE", "10/08/2024");
dt.Rows.Add(4, "INACTIVE", "10/12/2023");
dgvAccounts.DataSource = dt;
foreach (DataGridViewRow dr in dgvAccounts.Rows)
{
if (dr.Cells["FromDate"].Value != null)
{
if (Convert.ToDateTime(dr.Cells["FromDate"].Value) <= DateTime.Today)
{
dr.DefaultCellStyle.ForeColor = Color.Red;
}
else
{
dr.DefaultCellStyle.ForeColor = Color.Green;
}
}
}
}
VB.Net
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {
New DataColumn("Id"),
New DataColumn("accountstatus"),
New DataColumn("FromDate")})
dt.Rows.Add(1, "INACTIVE", "10/12/2023")
dt.Rows.Add(2, "INACTIVE", "11/12/2023")
dt.Rows.Add(3, "ACTIVE", "10/08/2024")
dt.Rows.Add(4, "INACTIVE", "10/12/2023")
dgvAccounts.DataSource = dt
For Each dr As DataGridViewRow In dgvAccounts.Rows
If dr.Cells("FromDate").Value IsNot Nothing Then
If Convert.ToDateTime(dr.Cells("FromDate").Value) <= DateTime.Today Then
dr.DefaultCellStyle.ForeColor = Color.Red
Else
dr.DefaultCellStyle.ForeColor = Color.Green
End If
End If
Next
End Sub
Screenshot