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 に答える