Recently I was looking for a solution to this quandary for an application I was working on. Based on this AdamSpeight2008 came up with a VB.NET solution to the issue and posted it on his blog, so I thought I'd show the C# solution I came up with.
I do believe in some aspects Adam's solution may be more efficient, as it uses a built-in event for the ListView Control, but I thought it an interesting enough quandary to come up with my own solution, so here it is
Happy coding, and can you come up with your own solution?
I do believe in some aspects Adam's solution may be more efficient, as it uses a built-in event for the ListView Control, but I thought it an interesting enough quandary to come up with my own solution, so here it is
/// <summary>
/// method for adding alternating row colows in a ListView control
/// </summary>
/// <param name="lst">ListView we're working with</param>
/// <param name="color1">furst color to use</param>
/// <param name="color2">second color to use</param>
public void SetAlternatingRowColors(ListView lst, Color color1, Color color2)
{
//loop through each ListViewItem in the ListView control
foreach (ListViewItem item in lst.Items)
{
if ((item.Index % 2) == 0)
item.BackColor = color1;
else
item.BackColor = color2;
}
}
Happy coding, and can you come up with your own solution?
3 Comments On This Entry
Page 1 of 1
girasquid
11 January 2010 - 02:31 PM
Doesn't C# have a ternary operator? I'd probably use a ternary for something this small, like so:
..more of a style quibble than anything, though.
item.BackColor = item.Index % 2 == 0 ? color1 : color2;
..more of a style quibble than anything, though.
Page 1 of 1
Trackbacks for this entry [ Trackback URL ]
Tags
- .Net
- .Net Framework 4.0
- 5 stages of data loss
- 52 Weeks Challenges
- 52 Weeks of Code
- Apple
- ASP.NET
- C#
- C# 4.0
- C# 64-Bit support
- C#4.0
- cross-thread exception
- data linking
- extension methods
- General Life
- General Programming
- INI files
- Iron Python
- jQuery
- Linux
- Mac vs PC
- Microsoft
- MSSQL
- P/Invoke
- Parallel Programming
- Ping class
- Rantings
- System.NetworkInformation Namespace
- Visual Studio 2010
- Windows 7
Recent Entries
Recent Comments
Search My Blog
1 user(s) viewing
1 Guests
0 member(s)
0 anonymous member(s)
0 member(s)
0 anonymous member(s)
My Blog Links
Blog Roll
Martyr2
SixOfEleven
SwiftStriker00
DogStopper
Lemur
erik.price
Core
KYA
born2c0de
More will be coming soon
SixOfEleven
SwiftStriker00
DogStopper
Lemur
erik.price
Core
KYA
born2c0de
More will be coming soon
|
|



3 Comments









|