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

后端 未结 6 991
天涯浪人
天涯浪人 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 05:00

    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.

提交回复
热议问题