简体   繁体   English

更改子项目背景颜色C#

[英]Changing subitem background color c#

I'm trying to change the background color of an especific column from my listview object, in C#. 我正在尝试从C#中的listview对象更改especific列的背景颜色。

I have just two columns: the first is called "Sequence" and the other one is called "Residue". 我只有两列:第一列称为“序列”,另一列称为“残基”。 The second one, called "Residue", is the column i want to "paint". 第二个称为“残留”,是我要“绘制”的列。

The code I'm using just change the full row background and not the column "Residue". 我使用的代码只是更改整行背景,而不是列“ Residue”。

Hope anyone can help! 希望任何人都能提供帮助!

Thanks a lot. 非常感谢。

Here is my code: 这是我的代码:

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);

If you're trying to paint the second row, then you'll need to use SubItems[1]. 如果要绘制第二行,则需要使用SubItems [1]。 As for painting, add "lvi.UseItemStyleForSubItems = false;" 至于绘画,请添加“ lvi.UseItemStyleForSubItems = false;”。 as seen below 如下所示

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM