?
Solved

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

Posted on 2008-06-19
10
Medium Priority
?
2,566 Views
Last Modified: 2012-05-05
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?
0
Comment
Question by:nirisan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 11

Accepted Solution

by:
William earned 1500 total points
ID: 21826576
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21827006
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
 

Author Comment

by:nirisan
ID: 21840170
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21841427
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
 

Author Comment

by:nirisan
ID: 21844619
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
 

Author Comment

by:nirisan
ID: 21844642
I am using Visual Studio 2003
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21846341
could you post a screenshot of the properties window?
0
 
LVL 11

Expert Comment

by:William
ID: 21847234
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 21847304
hmm, sorry, I stopped to work with VS 2003 many years ago ;)
0
 

Author Comment

by:nirisan
ID: 21853968
Please check attached jpg
events.JPG
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question