C# winforms: How do i give alternating colors to a listview which has data from a dataset

I have added a listview to the a winform which displays data from a dataset.

I would like to display the rows in alternating colors. How could i do this?
nirisanAsked:
Who is Participating?
 
WilliamConnect With a Mentor Commented:
I think after you load it you could use something simple to iterate thru the list....
 int i = 0;
 
            foreach ( ListViewItem item in this.listView1.Items )
            {
                i++;
                if ( IsEven( i ) )
                    item.BackColor = Color.Blue;
            }
//-----------------------------
The IsEven function
  public static bool IsEven( int intValue )
        {
            return ( ( intValue & 1 ) == 0 );
        }

Open in new window

0
 
Jaime OlivaresSoftware ArchitectCommented:
there is another approach. Override the DrawItem event, declare the event at the form designer and implement it:

void listView1_DrawItem(object sender, DrawListViewItemEventArgs e)
{
     e.DrawDefault = true;
     e.Item.BackColor = e.ItemIndex & 1 ? Color.White : Color.Cyan;
}

so this way items will be colored automatically, even if you add more rows.
0
 
nirisanAuthor Commented:
Hi Jaime

Could you please give an example how to override the DrawItem event.

As i need to add more rows, i think this would be appropriate.

thanks
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Jaime OlivaresSoftware ArchitectCommented:
as I told you, you have to go to the form designer, select the control you want, look at properties window (or right button, Properties). Select the event view (the button with the ligthning bolt). You will see all the possible events for this controls. Choose DrawItem and double click in the blank space, an empty drawitem method should appear in your code. Fill it as explained.
0
 
nirisanAuthor Commented:
Jamie,

I did exactly as you told. But I could not find DrawItem in the events( When i selected the listview control and selected the properties and the lightning bulb, where all the events were listed)??
0
 
nirisanAuthor Commented:
I am using Visual Studio 2003
0
 
Jaime OlivaresSoftware ArchitectCommented:
could you post a screenshot of the properties window?
0
 
WilliamCommented:
In .net 1.1 for the listview item there is no DrawItem event....  (exposed) dont know if there is a protected one inside it....  :( there is not - just disassembled it....

http://msdn.microsoft.com/en-us/library/system.windows.forms.listview_events(VS.71).aspx

jamie -> Is it possible to the current item in the paint event?
0
 
Jaime OlivaresSoftware ArchitectCommented:
hmm, sorry, I stopped to work with VS 2003 many years ago ;)
0
 
nirisanAuthor Commented:
Please check attached jpg
events.JPG
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.