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.
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;
}
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With