0

我的 . 有问题,我DataGridView使用 CellFormatting 方法根据行的内容重新着色。在大多数情况下,这似乎工作正常,直到 DGV 中的第一行返回 true - 在这种情况下,DGV 中的每一行都被涂成红色。

这是我的代码:

private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    DataGridViewRow theRow = MyData.Rows[e.RowIndex];
        if (theRow.Cells[4].Value.ToString() == "1")
        {
            ChangeRowColor(theRow, Color.PaleVioletRed);
        }
}

private void ChangeRowColor(DataGridViewRow r, Color c)
{
    r.DefaultCellStyle.BackColor = c;
}

编辑:解决方案:

private void MyData_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        DataGridViewRow theRow = MyData.Rows[e.RowIndex];
        if ((int)theRow.Cells[4].Value == 1)
        {
            e.CellStyle.BackColor = Color.PaleVioletRed;
        }
}
4

1 回答 1

0

您应该将背景色分配给事件中的 CellStyle。

于 2016-11-03T12:33:13.943 回答