更改子项背景颜色 c#

Changing subitem background color c#

我正在尝试在 C# 中更改列表视图对象中特定列的背景颜色。

我只有两列:第一列称为 "Sequence",另一列称为 "Residue"。第二个,叫做 "Residue",是我想要 "paint".

的列

我使用的代码只是更改整行背景而不是列 "Residue"。

希望大家帮帮忙!

非常感谢。

这是我的代码:

for (int i = 0; i < Variables.NSeqSNP; i++)
        {
            char res = Variables.SequencesSNP[i].ToString()[pos];
            ListViewItem lvi = new ListViewItem(Variables.SeqNameSNP[i].ToString());
            lvi.SubItems.Add(res + " ");
            if (res == 'A') lvi.SubItems[0].BackColor = Color.Blue;
            else if (res == 'T') lvi.SubItems[0].BackColor = Color.Red;
            else if (res == 'C') lvi.SubItems[0].BackColor = Color.Green;
            else if (res == 'G') lvi.SubItems[0].BackColor = Color.Yellow;

            lstOutputSNP.Items.Add(lvi);

如果您尝试绘制第二行,则需要使用 SubItems[1]。至于绘画,添加"lvi.UseItemStyleForSubItems = false;"如下所示

for (int i = 0; i < Variables.NSeqSNP; i++)
{
    char res = Variables.SequencesSNP[i].ToString()[pos];
    ListViewItem lvi = new ListViewItem(Variables.SeqNameSNP[i].ToString());
    lvi.SubItems.Add(res + " ");
    lvi.UseItemStyleForSubItems = false;
    if (res == 'A') lvi.SubItems[1].BackColor = Color.Blue;
    else if (res == 'T') lvi.SubItems[1].BackColor = Color.Red;
    else if (res == 'C') lvi.SubItems[1].BackColor = Color.Green;
    else if (res == 'G') lvi.SubItems[1].BackColor = Color.Yellow;

    lstOutputSNP.Items.Add(lvi);
}