Search code examples
c#objectlistview

How can I change ObjectListView row backcolor?


I have an objectlistview from BrightIdeasSoftware. Currently I can add and remove to this list however I can't paint my row colors(NOT HEADER) Simply I want to re color half of my list to red and rest to blue as example.

Normally I would do this :

            for (int i = 0; i < index; i++)
            {
                mainForm.MyListView.Items[i].BackColor = Color.LightGray;
            }
            mainForm.MyListView.Items[index].BackColor = Color.DarkGreen;
            for (int i = index; i < mainForm.MyListView.Items.Count; i++)
            {
                mainForm.MyListView.Items[i].BackColor = Color.FromArgb(18, 18, 18);
            }

But this is not working, I have also tried refreshing object after re color them but still not working. I have checked this but I don't want to do it with a condition I just want to give an index and then re color my listview.

Can someone show me how can I achieve this? Thanks a lot

EDIT : I will share my whole method so it will be clearer..

public void PaintToIndex(int index)
        {
            for (int i = 0; i < index; i++)
            {
                mainForm.MyListView.Items[i].BackColor = Color.LightGray;
            }
            mainForm.MyListView.Items[index].BackColor = Color.DarkGreen;
            for (int i = index; i < mainForm.MyListView.Items.Count; i++)
            {
                mainForm.MyListView.Items[i].BackColor = Color.FromArgb(18, 18, 18);
            }

        }

EDIT2: I think I might found something, I have change my method to this but it's updating itself back.

            for (int i = 0; i < index; i++)
            {
                OLVListItem CurItem = mainForm.MyListView.GetItem(i);
                CurItem.BackColor = Color.LightGray;
                //mainForm.MyListView.RefreshItem(CurItem);
            }
            mainForm.MyListView.GetItem(index).BackColor = Color.LightGray;
            for (int i = index; i < mainForm.MyListView.Items.Count; i++)
            {
                OLVListItem CurItem = mainForm.MyListView.GetItem(i);
                CurItem.BackColor = Color.FromArgb(18, 18, 18);
                //mainForm.MyListView.RefreshItem(CurItem);
            }

When I open RefreshItem it update my OLVListItem back to previous color..

EDIT 3: I found the solution. I did Refresh() after set all my colors but now I have another problem, when I hover with my mouse the color is changing back..


Solution

  • Okay I found the solution. This is how I did it

                int CurrentIndex = StaticVariables.MyListView.GetPlaylistCurrentIndex();
                int count = StaticVariables.MyListView.GetPlaylistCount();
                for (int i = 0; i < CurrentIndex; i++)
                {
                    OLVListItem item = mainForm.MyListView.GetItem(i);
                    item.BackColor = Color.FromArgb(35, 35, 35);
                }
                for (int i = CurrentIndex; i < count; i++)
                {
                    OLVListItem item = mainForm.MyListView.GetItem(i);
                    item.BackColor = Color.FromArgb(18, 18, 18);
                }
                OLVListItem item2 = mainForm.MyListView.GetItem(CurrentIndex);
                item2.BackColor = Color.DarkGreen;
                mainForm.MyListView.Refresh();
    

    I call this method on FormatRow event. There is 1 more thing I want to mention. This was not working until I checked UseHotControls to false. You know this property do some fancy things when you hover your mouse over the cell or row or whatever but I guess it's not working well with back color changes because when it was true(as default) my ObjectListView was not updating it's back color until I move my mouse over OLV or click any item but then when I was hovering and activating HotControl they were changing their color back to original(Transparent). I manage to change HotControl back color but then still I had the issue with not updating itself. After I set UseHotControls to false and call same method everything work perfectly. I'll leave this method and this long paragraph here in case if someone else is going to need it.