0

So I'm using C# in a Windows Forms Application and my question here is about the ListView. All I want to do is display the ListViewItem in a certain colour depending on the value. Now I have tried the traditional method of:

ListViewItem li = new ListViewItem();
li.ForeColor = Color.Green;
li.Text = "Limit: " + wc.getCreditLimit();
listLimits.Items.Add(li);

But the result I see in the ListView is:

ListViewItem:{Limit:15000}

And no, it's not coloured

Another method I tried was to initialise the ListViewItem with text as an argument like so:

ListViewItem li = new ListViewItem("Limit: " +   wc.getCreditLimit());
li.ForeColor = Color.Green;
listLimits.Items.Add(li);

But that also produces the same result as before.

Another Method I tried was do add the Text property of the ListViewItem like so:

ListViewItem li = new ListViewItem("Limit: " + wc.getCreditLimit());
li.ForeColor = Color.Green;
listLimits.Items.Add(li.Text);

Now this displays the correct text in the ListView, however It's still not coloured!

Can someone please explain this odd behaviour because from what I've read, it looks like this is the only approach to adding colour to a ListViewItem. Any advice would be appreciated, thanks.

Raktim Biswas
  • 4,011
  • 5
  • 27
  • 32
Jalapeno
  • 202
  • 1
  • 2
  • 9
  • Cheers for the down-vote... now if someone could try to help rather than criticise that would be great! – Jalapeno Aug 01 '16 at 11:34
  • According to [this](http://stackoverflow.com/q/3400146/1997232) you did it right. I suspect a usual *user mistake*, perhaps `ListView` is not in `Details` mode or something else. As for *"it looks like this is the only approach to adding colour to a ListViewItem"* you are incorrect, there are other *approaches*: 1) WPF 2) [owner drawn](http://stackoverflow.com/q/9066408/1997232) ListView 3) third-party component. – Sinatr Aug 01 '16 at 11:50

2 Answers2

1

Your code (first paragraph) works just fine.

If you see ListViewItem:{Limit:15000} you have a problem elsewhere.

It looks as if you added a wrong object type, but as it clearly is a ListViewItem you don't..

So I guess you are adding it to a ListBox instead of a ListView.. When I do that I get what you see:

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
0

Try this:

listLimits.Items.Add(li);

You are adding just the text not ListViewItem

listLimits.Items.Add(**li.Text**);