Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Question about a listview

Dear experts,

I have a listview on my form and when I press a button the methode
SetAlternateItemColours will be executed. This methode color the rows
of the listview. I like to hang this methode to the event of the listview.
Is that possible? And what event works?

And I like to use the color: $F9F9F9 instead of the WhiteSmoke.

Who knows the solutions and is willing to help me?

Peter


private void button3_Click(object sender, EventArgs e)
            {
         SetAlternateItemColours();
            }

        private void SetAlternateItemColours()
        {
            for (int i = 0; i <= lstvGlucose.Items.Count - 1; i += 1)
            {
                if (i % 2 == 0)
                {
                    lstvGlucose.Items[i].BackColor = System.Drawing.Color.White;
                }
                else
                {
                    lstvGlucose.Items[i].BackColor = System.Drawing.Color.WhiteSmoke;
                }
            }
        }

Open in new window

Avatar of Peter Kiers
Peter Kiers
Flag of Netherlands image

ASKER

I allready got the wanted colour.
Can now someone tell me what listview event to use?

P.
private void SetAlternateItemColours()
        {
            for (int i = 0; i <= lstvGlucose.Items.Count - 1; i += 1)
            {
                if (i % 2 == 0)
                {
                    lstvGlucose.Items[i].BackColor = System.Drawing.Color.White;
                }
                else
                {
                    lstvGlucose.Items[i].BackColor = #F9f9f9;
                }
            }
        }

Open in new window

Damn, this didn't work:

lstvGlucose.Items[i].BackColor = #F9f9f9;

Can someone help me?

P.
ASKER CERTIFIED SOLUTION
Avatar of AterMentis
AterMentis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ah, thanks AterMentis. your the best.

I have onle little question left. The points are yours anyway.
Is it possible to set the methode SetAlternateItemColours as an listview event.
If its possible, What event should I use.

Peter
Avatar of AterMentis
AterMentis

There's no such event, however you can assign colors upon adding items in your code or execute a function that will do so after items are added.

An easier path would be using the DataGridView which has the Alternating Rows function built in.
Oke, thanks for your solution.
Greetings, Peter Kiers
Very good solution
You're welcome :)