Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the backcolor of a listview subitem using its own value

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

like image 696
Rye Avatar asked Aug 25 '10 02:08

Rye


People also ask

How to change ListView Item Color in c#?

You can use the BackColor property to change the color displayed behind the item text. This property can be used if you want to use different background and foreground color combinations (using the ForeColor property to set the foreground color) to differentiate one item from another.

What is a ListView subitem?

The Windows Forms ListView control can display additional text, or subitems, for each item in the Details view. The first column displays the item text, for example an employee number. The second, third, and subsequent columns display the first, second, and subsequent associated subitems.


2 Answers

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.

like image 165
Josh Avatar answered Sep 22 '22 15:09

Josh


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);
}
like image 29
Leniel Maccaferri Avatar answered Sep 21 '22 15:09

Leniel Maccaferri