如何从下拉列表中删除多个项目?

时间:2015-05-20 06:49:59

标签: c# asp.net

这里我绑定了dropDownList。

我想从下拉列表中删除多于1项的PersonList。

此人员列表已被选中,我想从dropDownList数据中删除它。我怎么能这样做?

dt = AppNameApi.getStaffStudList();
ddl.DataTextField = "NAME";
ddl.DataValueField = "uid_nmbr";        

这里是从DropDownList中删除列表

foreach (GridViewRow row in gvSubjectChange.Rows)
{
    if (row.RowType == DataControlRowType.DataRow)
    {
        int index = row.RowIndex;        
        Label lbluid_nmbr = row.FindControl("labeluid_nmbr") as Label;            StudStaffList.Add(Convert.ToString(gvSubjectChange.DataKeys[row.RowIndex].Value));
        int t = StudStaffList.Count
        StudStaff = Convert.ToString(gvSubjectChange.DataKeys[row.RowIndex].Value);
        PersonList = string.Join(",", StudStaffList.ToArray());
        ViewState["PersonList"] = PersonList;
    }
}

代码:

ddl.Items.Add(new ListItem("Select", "0"));
dt = (DataTable)ViewState["StaffStudList"];
ddl.DataTextField = "NAME";
ddl.DataValueField = "uid_nmbr";
ddl.DataSource = dt;      
ddl.DataBind();           
string[] Plist = (string[])ViewState["plist"];                                      
foreach (string s in Plist)
{                       
    ddl.Items.Remove(ddl.Items.FindByValue(s));  
}      

2 个答案:

答案 0 :(得分:0)

确保您的StudStaffList.ToArray()正在存储SelectedValue

DropDownList
foreach (GridViewRow row in gvSubjectChange.Rows)
    {
        if (row.RowType == DataControlRowType.DataRow)
        {
            int index = row.RowIndex;

            Label lbluid_nmbr = row.FindControl("labeluid_nmbr") as Label;
            StudStaffList.Add(Convert.ToString(gvSubjectChange.DataKeys[row.RowIndex].Value));
            int t = StudStaffList.Count
            StudStaff = Convert.ToString(gvSubjectChange.DataKeys[row.RowIndex].Value);

            DropDownList ddl = row.FindCountrol("yourDropDownList") as DropDownList;

            int i = 0;
            foreach(var item in StudStaffList.ToArray()){
                ddl.Items.Remove(ddl.Items.FindByValue(StudStaffList[i]));
                i ++;
            }
        }
    }         

答案 1 :(得分:0)

您需要在绑定之前过滤数据,例如

string[] Plist = (string[])ViewState["plist"];   
var dt = from row in ((DataTable)ViewState["StaffStudList"]).AsEnumerable()
     where !Plist.Contains(row["uid_nmbr"])
     select row;

ddl.DataTextField = "NAME";
ddl.DataValueField = "uid_nmbr";
ddl.DataSource = dt;      
ddl.DataBind();   
DataTable的

AsEnumerable扩展程序