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

后端 未结 6 950
天涯浪人
天涯浪人 2021-01-05 04:33

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

6条回答
  •  心在旅途
    2021-01-05 04:48

    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

提交回复
热议问题