Datagridview 基于数据的行格式化

2 c# datagridview winforms

我有一个绑定到 BindingSource 的 DataGridView。每行包含三个字段:名称、开始日期、结束日期。在我的窗体的其他地方,我有一个 DateTimePicker 控件。

当 DateTimePicker 的日期位于行的开始日期和结束日期之间时,我想突出显示该行。但我不知道该怎么做。谁能给我一个起点?

小智 5

如果您需要突出显示完整的行,那么您可能需要DefaultCellStyle在 ForEach 循环中使用每行的属性,或者您可以使用CellFormatting事件,如下所示:

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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)