Winforms - How to alternate the color of rows in a ListView control?
Asked Answered
P

6

9

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;
}
Paragraphia answered 19/5, 2010 at 15:5 Comment(2)
As an aside, zebra striping may or may not make your table any easier to read according to this A List Apart article. alistapart.com/articles/zebrastripingdoesithelpThroughput
+1 - Great article @Timothy. I'll go with Zebra striping.Paragraphia
M
5

I'm afraid that is the only way in Winforms. XAML allows this through use of styles though.

Moo answered 19/5, 2010 at 15:11 Comment(0)
A
8

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.

Agha answered 13/12, 2017 at 6:11 Comment(0)
M
5

I'm afraid that is the only way in Winforms. XAML allows this through use of styles though.

Moo answered 19/5, 2010 at 15:11 Comment(0)
D
0

As far as I know WPF allows to set style on any control using <Styles/> But in winforms I'm afraid may be that's the only way.

Dido answered 20/5, 2010 at 5:11 Comment(0)
G
0
        for (int i = 0; i <= listView.Items.Count - 1; i = (i + 2))
        {
            listView.Items[i].BackColor = Color.Gainsboro;
        }

Set the main background in the properties menu then use this code to set the alternate color.

Gomez answered 28/2, 2018 at 10:47 Comment(0)
S
-1

You can also take advantage of owner drawing, rather than setting properties explicitly. Owner drawing is less vulnerable to item reordering.

Here is how to do this in Better ListView (a 3rd party component offering both free and extended versions) - its a matter of simply handling a DrawItemBackground event:

private void ListViewOnDrawItemBackground(object sender, BetterListViewDrawItemBackgroundEventArgs eventArgs)
{
    if ((eventArgs.Item.Index & 1) == 1)
    {
        eventArgs.Graphics.FillRectangle(Brushes.AliceBlue, eventArgs.ItemBounds.BoundsOuter);
    }
}

result:

enter image description here

Schoen answered 22/4, 2014 at 22:43 Comment(0)
A
-2

Set the ListView OwnerDraw property to true and then implement the DrawItem handler. Have a look here : Winforms - How to alternate the color of rows in a ListView control?

Agha answered 13/12, 2017 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.