Monday, October 19, 2009

DropdownList Apply Colors for Category

if(!ispostback)
{
BindDll()
}


void BindDll()
{
SqlConnection mycn;
SqlDataAdapter myda;
DataTable dt;
String strConn;
strConn = ConfigurationManager.ConnectionStrings["connString"].ConnectionString;
mycn = new SqlConnection(strConn);
myda = new SqlDataAdapter ("Select * FROM Category", mycn);
dt = new DataTable();
myda.Fill(dt);
DropDownList1.DataSource = dt;
DropDownList1.DataTextField = "CategoryName";
DropDownList1.DataValueField ="CategoryId";
DropDownList1.DataBind () ;
for (int i =0;i < DropDownList1.Items.Count ;i++ )
{
DropDownList1.Items[i].Attributes.Add("style", "color:" + dt.Rows[i]["Color"].ToString());
}
}

If the ddl is available in a grid set the property on grid onRowDatabound.

Copyright © 2009 Angel