Using C# Winforms (3.5).
Is it possible to set the row colors to automatically alternate in a listview?
Or do I need to manually set the row color each time a new row is added to the listview?
Based on a MSDN article the manual method would look like this:
//alternate row color
if (i % 2 == 0)
{
lvi.BackColor = Color.LightBlue;
}
else
{
lvi.BackColor = Color.Beige;
}
Set the ListView OwnerDraw property to true and then implement the DrawItem handler :
private void listView_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.DrawDefault = true;
if ((e.ItemIndex%2) == 1)
{
e.Item.BackColor = Color.FromArgb(230, 230, 255);
e.Item.UseItemStyleForSubItems = true;
}
}
private void listView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
{
e.DrawDefault = true;
}
This example is a simple one, you can improve it.
I'm afraid that is the only way in Winforms. XAML allows this through use of styles though.
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