c# Datagridview (Winform) 基于相邻单元格值的单元格着色

标签 c# winforms datagridview

是否可以根据多种条件为 datagridview 单元格着色。 我知道我可以根据该单元格值更改单元格的颜色。但是否可以添加条件,让我也可以根据相邻的单元格值应用颜色。

要将单元格的日期与当前日期进行比较,我使用下面的代码。

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DATE")
    {
        if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0)
        {
            return;
        }
        else
        {
            if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date)
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.ForeColor = Color.White;
            }
        }
    }
    // This section change the color of action proposed description column cell.
    // i want to change the color in "ACTION PROPOSED DATE"column, if "ACTION PROPOSED DESCRIPTION" contains file closed
    else if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DESCRIPTION")
    {
        if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0)
        {
            return;
        }
        else
        {
            string stringvalue = (string) e.Value;
            stringvalue = stringvalue.ToLower();
            if ((stringvalue.IndexOf("file closed") > -1))
            {
                e.CellStyle.BackColor = Color.Purple;
            }
        }
    }
}

如果“建议行动描述”包含“文件关闭”,我想将“建议行动日期”列单元格中的颜色更改为紫色

这是我在datagridview中得到的结果

present output

这是我期待的结果

expected result

在发布之前,我用谷歌搜索了很多,但没有找到我的问题的任何答案。所以我希望我没有重复这个问题。

最佳答案

CellFormatting 事件的事件参数指示正在格式化 的单元格。但是您可以使用其他单元格来确定如何设置该单元格的格式。

例如,为了实现你的目标,你可以使用这样的东西:

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
    if (dataGridView1.Columns[e.ColumnIndex].Name == "ACTION PROPOSED DATE")
    {
        // Take the other column value for the same row
        var proposedDescription = dg.Rows[e.RowIndex].Cells["ACTION PROPOSED DESCRIPTION"].Value as string;
        if (proposedDescription != null && proposedDescription.IndexOf("file closed", StringComparison.CurrentCultureIgnoreCase) >= 0)
        {
             e.CellStyle.BackColor = Color.Purple;
             return;
        }
        if (e.Value == null || e.Value == System.DBNull.Value || e.ColumnIndex < 0 || e.RowIndex < 0)
        {
            return;
        }
        else
        {
            if (((DateTime) e.Value).Date < (DateTime) DateTime.Now.Date)
            {
                e.CellStyle.BackColor = Color.Red;
                e.CellStyle.ForeColor = Color.White;
            }
        }
    }
}

关于c# Datagridview (Winform) 基于相邻单元格值的单元格着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36327064/

相关文章:

c# - 在影响模型的屏幕上书写文本

c# - 调用子窗体的 Hide 方法后窗体失去焦点

.net - 将按钮控件添加到 SplitContainer Splitter

c# - 无法在 .Net Core RC2 中添加 View

c# - 发布到其他人的 Azure 订阅?

c# - 如何使 TextBox 随着用户的输入不断更新?

c# - 需要有关包含许多列的可编辑 WinForms 表格的建议,还有……这个小部件是什么?

c# - 在 c# 中从数据库检索数据到 dataGridView 时出现空行

c# - 如何为DataGridview RowHeader设置Header?

c# - 使用 ReadByte() 读取泛型类型