Alternate Color in ListView C# (.Net 3.5)?

前端 未结 2 1816
情深已故
情深已故 2020-12-19 22:41

I\'m gonna set an alternate color to my ListView rows.
I saw this link but I\'m using .Net Framework 3.5 SP1 , so I can\'t use it.

I\'ve used the following

相关标签:
2条回答
  • 2020-12-19 23:24

    Here is a easy way to do it -> Alternate background

    0 讨论(0)
  • 2020-12-19 23:32

    It is quite unclear from your snippet, but I'll guess you want alternating colors. Even numbered items colored one way, odd numbered colored another way. Yes, very effective as a reading guide when you have a large number of columns in the view.

    And yes, that's going to get mucked up when you sort the items. Right after sorting, you'll need a simple for loop that changes the BackColor property.

        private static void recolorListItems(ListView lv) {
            for (int ix = 0; ix < lv.Items.Count; ++ix) {
                var item = lv.Items[ix];
                item.BackColor = (ix % 2 == 0) ? Color.Beige : Color.White;
            }
        }
    

    Call this after sorting. Or after filling the ListView. I suck at colors, please pick your own.

    0 讨论(0)
提交回复
热议问题