22

How can I programmatically change the back color of a single cell in a listview using its own value?

alt text

The values in the ColorFlag Column Came from the database.

Here is my code:

foreach(DataRow dr in _dataTbl.Rows) 
        {
            _markOW = dr["Mark"].ToString();
            _stock = dr["Stock"].ToString();
            _SteelSectio = dr["SteelSection"].ToString();
            _colo = (Int32)dr["Color"];


            ListViewItem _lvi = new ListViewItem(_markOW);
            _lvi.SubItems.AddRange(new string[]{_SteelSectio, _stock,     _colo.ToString()});

            _myListView.Items.Add(_lvi);   }

Here is the code that I have tried to change the backcolor of the cells:

for (int _i = 0; _i < _owLV.Items.Count; _i++)
            {
                _myListView.Items[_i].UseItemStyleForSubItems = false;
                _myListView.Items[_i].SubItems[3].BackColor = Color.FromArgb(_colo);
            }

Thanks in advance

Rye
  • 2,273
  • 9
  • 34
  • 43
  • Why aren't you using a DataGridView? – siride Aug 25 '10 at 02:18
  • I don't know how to use DataGridView yet. – Rye Aug 25 '10 at 02:20
  • Well, this might be a good time to learn. It's actually not that difficult and the visual designer tools for setting up a simple gridview are fairly straightforward. I think your time would be better spent learning that than fighting with ListView to make it do what it's not really designed to do. – siride Aug 25 '10 at 02:24
  • DataGridView is a horrible control and unless you're using it as an editable bound data grid, there's really no purpose to it. A much better alternative to ListView would be ObjectListView. – Josh Aug 25 '10 at 02:32
  • Thanks josh. It's like a stupid mistake. – Rye Aug 25 '10 at 02:51
  • Do you want to qualify your statement about DataGridView? I've used it for quite a number of things and found it to be very flexible, both bound to a data source and not. – siride Aug 25 '10 at 03:21

2 Answers2

43

As far as I can tell, the code you have looks fine. I just threw together a quick Windows Forms application and tossed a ListView on the form with two columns in detail view. The following code works fine.

var item1 = new ListViewItem( "Item 1");
item1.SubItems.Add( "Color" );
item1.SubItems[1].BackColor = Color.FromArgb( -16711936 );
item1.UseItemStyleForSubItems = false;

listView1.Items.Add( item1 );

I would try setting the BackColor before you add the item. It also looks like you're setting all the items to the same color which is probably not what you want.

Josh
  • 68,005
  • 14
  • 144
  • 156
  • 2
    Great solution! `UseItemStyleForSubItems` is essential. And the indexing can be eliminated by first creating a `ListViewItem.ListViewSubItem` object, coloring its background, and then adding it to `.SubItems.Add()` call. – Roland Feb 18 '16 at 16:18
5

Take a look at these links:

C# ListView Detail, Highlight a single cell

Changing color of list view cell using C# (has a working solution)

The key point is to set this:

listView1.Items[0].UseItemStyleForSubItems = false;

Do this:

foreach (DataRow dr in _dataTbl.Rows)
{
    _markOW = dr["Mark"].ToString();
    _stock = dr["Stock"].ToString();
    _SteelSectio = dr["SteelSection"].ToString();
    _color = (Int32)dr["Color"];

    ListViewItem _lvi = new ListViewItem(_markOW);

    _lvi.SubItems.AddRange(new string[] {_SteelSectio, _stock, _color.ToString() });    
    _lvi.UseItemStyleForSubItems = false;
    _lvi.SubItems[2].BackColor = Color.FromArgb(_color);

    _myListView.Items.Add(_lvi);
}
Community
  • 1
  • 1
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • The second link you have posted is the one I use a while ago. But the problem with that, is that the last value set in the variable _colo will be the back color of the column. – Rye Aug 25 '10 at 02:40
  • 1
    @Rye that's because you are setting them all the to same color, _colo. Take a look at my example and my note at the end. You should set the BackColor before you add the item. – Josh Aug 25 '10 at 02:43
  • *UseItemStyleForSubItems* -- that's the key to getting the background color change on the subitems -- thanks, this really helped! – Kit Dec 09 '10 at 18:23