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
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");
}
}
}
}