重复组合框

时间:2011-04-01 06:40:16

标签: c# .net winforms combobox

嘿伙计们,实际上我有两个具有相同元素的组合框但是我使用了两个具有相同元素的数据集。现在当我用数据集绑定组合框时,它重复了它的元素。 任何人都可以帮我解决一下吗?

我的代码是这样的:

DataSet ds_PromotionDesignation = new DataSet();
ds_PromotionDesignation = EPI.comboDeg();
cmbPromotionDesignationFrom.DataSource = ds_PromotionDesignation.Tables[0];
cmbPromotionDesignationFrom.DisplayMember = "DEG_NAME";
cmbPromotionDesignationFrom.ValueMember = "DEG_ID";
cmbPromotionDesignationFrom.SelectedIndex = -1;

DataSet ds_PromotionDesignationTo = new DataSet();
ds_PromotionDesignationTo = EPI.PromotionDesignationTo();
foreach (DataRow row in ds_PromotionDesignationTo.Tables["tbl_org_Desg"].Rows)
{
    myAL.Add(new USState(row["DEG_ID"].ToString(),row["DEG_NAME"].ToString()));
 }
 cmbPromotionDesignationTo.DataSource = myAL;
 cmbPromotionDesignationTo.DisplayMember = "DEGNAME";
 cmbPromotionDesignationTo.ValueMember = "DEGID";

1 个答案:

答案 0 :(得分:1)

查看您的代码,看起来您希望将项目的所有权从一个人转移到另一个人,使用两个组合填充相同的数据列表。

您想要从目标组合中排除所选项目。尝试使用foreach循环,只添加一个项目,如果它与当前选择的值不同,就像这样(未经测试)

       foreach (DataRow row in ds_PromotionDesignationTo.Tables["tbl_org_Desg"].Rows)
        {
            if ((int)row["DEG_ID"] != (int)cmbPromotionDesignationFrom.SelectedValue)
            {
                myAL.Add(new USState(row["DEG_ID"].ToString(), row["DEG_NAME"].ToString()));
            }
        }