Microsoft Communities

Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

How do I programmatically change the ForeColor and BackColor properties of subitems in a ListView?

I am trying to programmatically change the ForeColor and BackColor properties of subitems in a ListView. Why doesn't this work?

Make sure the item's UseItemStyleForSubItems property is set to false.

ListViewItem item1 = new ListViewItem( "item1", 0 );

item1.UseItemStyleForSubItems = false;  // this line makes things work

// set fore & back color of next sub item
item1.SubItems.Add( "1", Color.Black, Color.LightGreen, Font );
item1.SubItems.Add( "44" );
item1.SubItems.Add( "3" );

// As long as UseItemStyleForSubItems is false,
// you can later set the colors with code such as
item1.SubItems[ 2 ].BackColor = Color.Pink;

Contributed from George Shepherd's Windows Forms FAQ



Page view counter