我在这个问题中看到了很多例子,但直到没有结束。我尝试了所有这些EXAMPLES。我试图删除从数据库绑定的空复选框列表。
DataSet ds4 = getCheckBox(ViewState["id"].ToString());
chkEnvironment.DataSource = ds4;
chkEnvironment.DataTextField = "Environment";
chkEnvironment.DataValueField = "Environmentid";
chkEnvironment.DataBind();
但它在我的页面中显示空白复选框。该怎么做
protected void chkEnvironment_DataBound(object sender, EventArgs e)
{
foreach (ListItem item in chkEnvironment.Items)
{
if (item.Value == "NULL")
{
chkEnvironment.Items.Remove(item);
}
}
}
答案 0 :(得分:1)
您无法在枚举期间修改集合。您可以存储以后要删除的项目:
var itemsToRemove = new List<ListItem>();
foreach (ListItem item in chkEnvironment.Items)
{
if (item.Value == "NULL")
{
itemsToRemove.Add(item);
}
}
foreach(var item in itemsToRemove)
chkEnvironment.Items.Remove(item);
与LINQ相同:
var itemsToRemove = chkEnvironment.Items.Cast<ListItem>()
.Where(i => i.Value == "NULL")
.ToList();
foreach(var item in itemsToRemove)
chkEnvironment.Items.Remove(item);