我需要更改动态绑定下拉列表中特定列表项的背景颜色。需要突出显示的特定项目也是动态的并存储在数据集中。这样用户就知道该项目已经在程序的其他地方被选中,但如果他们愿意,仍然可以选择它。
下面是我的代码:
if ((e.Row.RowState & DataControlRowState.Edit) > 0)
{
string strWeek = ((Label)e.Row.FindControl("lblWeek")).Text.ToString();
string strBed = ((Label)e.Row.FindControl("lblBed")).Text.ToString();
string strJob = getSelectedJob(strBed, strWeek);
e.Row.BackColor = System.Drawing.ColorTranslator.FromHtml("#f8f8ff");
DataTable myTable = new DataTable();
DataColumn EIDColumn = new DataColumn("EID");
DataColumn EventNameColumn = new DataColumn("EventName");
myTable.Columns.Add(EIDColumn);
myTable.Columns.Add(EventNameColumn);
DataSet ds = new DataSet();
ds = getJobs(strBed, strWeek, ddlTerm.SelectedValue.ToString(), strJob);
DataSet ds2 = new DataSet();
ds2 = getAssignedJobs(strBed);
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddlEssential");
DataRow[] rows = ds.Tables[0].Select();
foreach (DataRow row in rows)
{
DataRow newRow = myTable.NewRow();
newRow["EID"] = row["EID"];
newRow["EventName"] = row["EventName"];
myTable.Rows.Add(newRow);
}
ddl.DataSource = myTable;
ddl.DataTextField = "EventName";
ddl.DataValueField = "EID";
ddl.DataBind();
ddl.Items.FindByValue(strJob).Selected = true;
foreach (DataRow row in ds2.Tables[0].Select())
{
string strAssignedJob = row["EID"].ToString();
ddl.Items.FindByValue(strAssignedJob).Attributes.Add("style", "background-color:#cccccc");
}
}
我收到此错误 对象引用未设置为对象的实例。它似乎发生在将样式属性添加到下拉列表的行上。
这甚至可能还是我的循环语句不正确?
非常感谢!