Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coloured ListBox Items

Status
Not open for further replies.

Dannybe2

Programmer
Jan 31, 2001
136
GB
Hi,

I am trying to change the properties of each item in a listbox so that I can have items of different colours.

I cannot seem to find a way of doing this, however I did find the following code, which suggests that I am looking for the attributes object, but it does not exist in my listbox. Is there any collections or anything I can include to my file so that it will exhibit these properties?

Thanks,
Dan

Code:
SqlConnection mycn;
SqlDataAdapter myda;
DataSet ds;
String strConn;
private void Page_Load(object sender, System.EventArgs e)
{
     if (!IsPostBack)
     {
          strConn="Data Source=localhost;uid=sa;pwd=;Initial Catalog=northwind";
          mycn = new SqlConnection(strConn);
          myda = new SqlDataAdapter ("Select * FROM Products ", mycn);
          ds = new DataSet();
          myda.Fill (ds,"Table");
                    
          for(int i = 0 ;i < ds.Tables[0].Rows.Count - 1;i++)
          {
               listBox1.Items.Add (new ListItem(ds.Tables[0].Rows[i]["UnitPrice"].ToString(),
               ds.Tables[0].Rows[i]["ProductID"].ToString()));
               if(Convert.ToDouble(ds.Tables[0].Rows[i]["UnitPrice"].ToString()) <= 25 )
               {
                    listBox1.Items[i].Attributes.Add("style", "color:red");
               }
               else
               {
                    listBox1.Items[i].Attributes.Add("style", "color:green");
               }
          }
     }
}
 
Is it in a windows app or is it a web app?

-Kevin

- "The truth hurts, maybe not as much as jumping on a bicycle with no seat, but it hurts.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top