我如何从下拉列表中删除超过1个项目

本文关键字:1个 项目 删除 下拉列表 | 更新日期: 2023-09-27 17:49:47

我在这里绑定下拉列表。

我想从下拉列表中删除超过1项的PersonList。

这个人列表是已经选择的人,我想从dropDownList数据中删除它。我怎么能做到呢?

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

这里是要从下拉列表中删除的列表

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

我如何从下拉列表中删除超过1个项目

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

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();   

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

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 ++;
            }
        }
    }