Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Varying ListViews item background color

Tags:

c#

listview

How can I make it so that a ListViews control's background color for items varies from item to item like in WinAmp, along with changing the column header colors?

http://i.stack.imgur.com/30pQy.png

If you look closely you can see the first item is a dark gray and the second is black and so on.

like image 584
user Avatar asked Oct 25 '10 08:10

user


2 Answers

You can set the ListViewItem.BackColor property, however this has to be done manually for each alternating line. Alternatively you could use a DataGridView which has an AlternateRowStyle property that would do this automatically - albeit you'll need to databind your rows in a collection of sorts which is a whole other topic.

For the simple case:

foreach (ListViewItem item in listView1.Items)
{
    item.BackColor = item.Index % 2 == 0 ? Color.Red : Color.Black;
}
like image 129
Andrew Hanlon Avatar answered Oct 20 '22 00:10

Andrew Hanlon


Handle the DrawItem event on the listbox and set the DrawMode to OwnerDrawVariable. The DrawItemEventArgs provides a BackColor property that can be set based on the index (also in the arg).

like image 36
Kell Avatar answered Oct 19 '22 23:10

Kell