2

I have a DataGridView bound to a BindingSource. Each row has three fields: Name, StartDate, EndDate. Elsewhere on my form, I have a DateTimePicker control.

When the DateTimePicker's date is between a row's start and end dates, I want to highlight that row. But I cannot figure out how to do this. Can anyone give me a starting point?

  • See this answer http://stackoverflow.com/a/4067612/581414 – Ruskin May 19 '16 at 10:41
  • @Ruskin this question has had an answer for 6 years and predates that one by a year, and the linked answer doesn't provide anything new. But thanks anyway. –  May 19 '16 at 14:19

2 Answers2

5

If you need the complete row to be highlighted then you may want to use the DefaultCellStyle property for each Row, either in a ForEach loop or you can use the CellFormatting Event as seen below:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (this.dataGridView1.Columns[e.ColumnIndex].Name == "Error")
    {
        if (e.Value != null && e.Value.ToString() == "1")
        {
             dataGridView1.Rows[e.RowIndex].DefaultCellStyle.ForeColor = Color.Red;
        }
    }
}
Java Devil
  • 10,629
  • 7
  • 33
  • 48
1

Do you want to highlight all of the rows that meet the condition?

You can go through row by row to see which rows satisfy the condition each time the DTP value changes.

Then, for each cell in these rows, change DataGridViewCell.Style however you want to highlight the rows. For each cell that is in a row that doesn't satisfy the constraint, set DataGridViewCell.Style to the default.

John
  • 15,990
  • 10
  • 70
  • 110