Varying ListViews item background color

c#, listview

Solution

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

Problem

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? If you look closely you can see the first item is a dark gray and the second is black and so on.

Original source