C# 更改datagridview中的元素

C# 更改datagridview中的元素,c#,winforms,datagridview,C#,Winforms,Datagridview,在过去的几天里,我一直在努力根据其中一个单元格中显示的日期更改datagridview中行的颜色。我在这里看过很多例子,但到目前为止没有一个能起到作用。以下是我现在拥有的: for (int i = 0; i < eolGrid.Rows.Count; i++) { DateTime dateval = Convert.ToDateTime(eolGrid.Rows[i].Cells[3].Value); if (datev

在过去的几天里,我一直在努力根据其中一个单元格中显示的日期更改datagridview中行的颜色。我在这里看过很多例子,但到目前为止没有一个能起到作用。以下是我现在拥有的:

for (int i = 0; i < eolGrid.Rows.Count; i++)
        {
            DateTime dateval = Convert.ToDateTime(eolGrid.Rows[i].Cells[3].Value);

            if (dateval <= date)
                eolGrid.Rows[i].Cells[3].Style.BackColor = Color.Red;
            else if (dateval <= date.AddDays(14))
                eolGrid.Rows[i].Cells[3].Style.BackColor = Color.Yellow;
            else
            {
                eolGrid.Rows[i].Cells[3].Style.BackColor = Color.Green;
            }
        }
for(int i=0;i如果(dateval我发现Ehsan回答了这个问题

根据我在代码中看到的,您正在更改默认颜色,这可能正在更改整个数据网格,而不仅仅是您想要的单元格


更改单元格内的样式更有意义,我从来没有这样做过,但可能是为了满足您的需要。

谢谢大家的帮助,我发现使用事件处理程序:“RowPostPaint”会在datagridview绘制完成后重新绘制每一行。如果有人感兴趣,这是我的代码:

    private void eolGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
        int rowNum = e.RowIndex;
        DataGridViewCellStyle style = new DataGridViewCellStyle();

        DateTime dateval = Convert.ToDateTime(eolGrid.Rows[rowNum].Cells[3].Value);

        if ((dateval - date).TotalDays <= 0)
            style.ForeColor = Color.Red;
        else if ((dateval - date).TotalDays <= 14)
            style.BackColor = Color.Red;
        else
            style.ForeColor = Color.Black;

        eolGrid.Rows[rowNum].Cells[3].Style = style;
    }
private void eolGrid_RowPostPaint(对象发送方,DataGridViewRowPostPaintEventArgs e)
{
int rowNum=e.RowIndex;
DataGridViewCellStyle=新DataGridViewCellStyle();
DateTime dateval=Convert.ToDateTime(eolGrid.Rows[rowNum].Cells[3].Value);

如果((dateval-日期).TotalDays请格式化您的代码,使其更易于阅读。此代码驻留在什么事件中?它是否位于
OnDataBound
事件中?您是否使用Debugger@David逐步完成代码?google如何使用
DataGridViewCellStyle
作为初学者。此外,这里还有一个您可以使用的前一个链接,例如您的代码看起来没问题。我建议在进行所有这些比较之前先拉出dateonce;这也会使调试更容易:
DateTime dateVal=Convert.ToDateTime(eolGrid.Rows[I].Cells[3].Value;
DateTime dateVal=(DateTime)(DGV.Rows[I].Cells[3].Value);
它在任何情况下都不存在,它位于主公共myForm()中。我已经仔细检查了代码,编译器也对其进行了检查,但当表单出现时,原始颜色不变。@MethodManThank you@Taw,代码现在看起来更干净了,但仍然不起作用:/n这不会编写整行代码,而只会编写一个单元格。OP的原始代码很好。同意,但用简单的foreach解决它,或者其他方法例如if(Convert.ToDateTime(eolGrid.Rows[i].Cells[3].Value)当然可以,但正如我所说的,原始代码是可以的,因此不需要编写循环。他应该可以解决实际问题。我尝试了这个方法,原始颜色仍然显示出来,尽管编译器确实运行了it@NicollasBraga我试过了,但没有成功
    private void eolGrid_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
    {
        int rowNum = e.RowIndex;
        DataGridViewCellStyle style = new DataGridViewCellStyle();

        DateTime dateval = Convert.ToDateTime(eolGrid.Rows[rowNum].Cells[3].Value);

        if ((dateval - date).TotalDays <= 0)
            style.ForeColor = Color.Red;
        else if ((dateval - date).TotalDays <= 14)
            style.BackColor = Color.Red;
        else
            style.ForeColor = Color.Black;

        eolGrid.Rows[rowNum].Cells[3].Style = style;
    }