I want to give different colors to alternate rows of listview Control in asp.net(c#).
How can i do this?

I am sure there are better ways but here is my method.

bool red = true;
            MyCon.Open();
            SqlDataReader search = search = new SqlCommand("SELECT something FROM somewhere WHERE condition", MyCon).ExecuteReader();
            ListViewItem temp;// = new ListViewItem();
            while (search.Read())
            {
                temp = new ListViewItem();
                temp.Text = search.GetString(0);
                temp.SubItems.Add(search.GetString(1));
               //etc.

                if (red)
                    temp.BackColor = Color.Red;
                else
                    temp.BackColor = Color.Green;

                red = !red; 

                listView1.Items.Add(temp);
            } 
            MyCon.Close();

Ofcourse you don't need to make it red and green.

Honestly red and Green are disgusting combo.

thanx finito.
But in which event of listview control i have to write this code.

when you are filling it.

Mark as solved if solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.