Border

Different colors for even/odd items in ListView

Andrej Stojkov
Lidor Systems

July 02, 2007

In some cases you want to have distinction in even and odd rows in your lists. In the following example we will show you how to create two different color styles for even and odd items, for clearer appearance of the list items.

// Color Style used for items with odd index number

LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle oddItemStyle = new LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle();

oddItemStyle.BackColor = Color.White;

oddItemStyle.TextColor = Color.FromArgb(107, 135, 208);

oddItemStyle.FillStyle = LidorSystems.IntegralUI.Style.FillStyle.Flat;

 

// Because some properties are the same, clone the color style

// from previous style and use it for items with even index number

LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle evenItemStyle = (LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle)oddItemStyle.Clone();

evenItemStyle.BackColor = Color.FromArgb(242, 242, 242);

for (int i = 0; i < this.listView1.Items.Count; i++)

{

// In order to use the created styles, clear the

// style inheritance from the parent ListView control

this.listView1.Items[i].StyleFromParent = false;

 

// Apply the color styles

if (i % 2 == 0)

this.listView1.Items[i].NormalStyle = oddItemStyle;

else

this.listView1.Items[i].NormalStyle = evenItemStyle;

}

' Color Style used for items with odd index number

Dim oddItemStyle As New LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle

oddItemStyle.BackColor = Color.White

oddItemStyle.TextColor = Color.FromArgb(107, 135, 208)

oddItemStyle.FillStyle = LidorSystems.IntegralUI.Style.FillStyle.Flat

 

' Because some properties are the same, clone the color style

' from previous style and use it for items with even index number

Dim evenItemStyle As LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle = DirectCast(oddItemStyle.Clone(), LidorSystems.IntegralUI.Lists.Style.ListItemColorStyle)

evenItemStyle.BackColor = Color.FromArgb(242, 242, 242)

For i As Integer = 0 To Me.listView1.Items.Count - 1

' In order to use the created styles, clear the

' style inheritance from the parent ListView control

Me.listView1.Items(i).StyleFromParent = False

 

' Apply the color styles

If i Mod 2 = 0 Then

Me.listView1.Items(i).NormalStyle = oddItemStyle

Else

Me.listView1.Items(i).NormalStyle = evenItemStyle

End If

Next

Different colors for even/odd items in ListView
Border