Click to See Complete Forum and Search --> : ListView Item loses backcolor


bewa
October 20th, 2004, 04:45 AM
Hi,

I create file list and place it in a listview. When I'm adding ListViewItems to the list, I make one line white background, next line grey for easy viewing. I use this code to do so:

ListViewItem l_lviFileData = new ListViewItem ( new string [] { fi.Name.ToString(), fi.LastWriteTime.ToString() } );
if ( l_iAddedItems%2 == 0 ) l_lviFileData.BackColor = Color.FromArgb ( 245 , 245 , 245 ); //VERY LIGHT GREY
p_lvListview.Items.Add ( l_lviFileData );

Works like a charm! But then I want to sort this list and after the sort, the coloring is (of course) completely whack. I get several lines of grey or white after another.

Does anyone has a good idea how to do this? I was thinking about a loop after the sort that would repeat the background coloring but it didn't work.

This, I tried:

int i = 0;
foreach ( ListViewItem lvi in lstFiles.Items)
{
lstFiles.Items.RemoveAt(i);
if ( i%2 == 0 ) lvi.BackColor = Color.FromArgb ( 245 , 245 , 245 ); //VERY LIGHT GREY
lstFiles.Items.Insert(i , lvi);
i++;
}

Any help or hints?

bewa
October 20th, 2004, 04:54 AM
hahahaaaaa!!! I found it!

Half of the lines were already grey and I was adding more grey lines to it without making the other white again.

This was the was to go:

int i = 0;
foreach ( ListViewItem lvi in lstFiles.Items)
{

if ( i%2 == 0 )
lvi.BackColor = Color.FromArgb ( 245 , 245 , 245 ); //VERY LIGHT GREY
else
lvi.BackColor = Color.FromArgb ( 255 , 255 , 255 ); //WHITE
i++;
}


That was a bit silly of me... :blush:

ABK
July 19th, 2005, 03:24 AM
Hi all,

I have a list view with HideSelection set to False.I have changed the backcolors of the listview items alternately(i.e zebra effect).Now if the user clicks in the list view itself in some place where there are no items , the selected listview item loses its backcolor.However it gets restored if someother item is selected in the list view.
This is reproducible in VS2003 on an XP machine.I tried overriding the OnPaint() method so that explicitly the backcolors are assigned to the list view items.But it was of no use.So is it a bug in ListView???

It wud really help if any body could tell me as to how to go about this problem
Thanks and Regards
Amita

ABK
July 19th, 2005, 03:44 AM
hi ,
I am trying to do something similar to what u have done above, altertnating the backcolors of the listview item.But the problem is if i click o0n the empty space in the list view the colors go fora toss.Now if i add a new entry to the list view the colors are not restored????

Andy Tacker
July 19th, 2005, 04:32 AM
You can also refer to this thread (http://www.codeguru.com/forum/showthread.php?t=348443)

Andy Tacker
July 19th, 2005, 04:34 AM
Merged two threads... I think that's ok with you ABK?

ABK
July 19th, 2005, 06:02 AM
Hi Andy,
The code listed shows how to change the color for the subitems.But my problem is a different one.Let me explain a little more
I change the list view item backcolors(alternately)i.e control and control light colors.This works fine.My requirement is that I have a button to add a new row.So when this is done the back color for the newly added row is updated accordingly.This too works fine
But the problem is before adding a new row if the selection is on the item having controls light color, if i have clicked on an empty space in the list view the selected item loses its backcolor.It will be restored if I click on some other item in the listview.I tried explicitly updating or refreshing the list view.But it was of no use.
So is this a bug in ListView???

rohit361
September 18th, 2007, 01:40 AM
hello friends,

i am also same kind of problem,

when i change a color of any row of list view, it will bw shown only when select other one and then select it back,

i have tried this with making hide selection true also,

but does get,

Do anyone has answer?