暂时隐藏Combobox中的项目

时间:2014-05-16 00:55:01

标签: c# combobox

我想知道是否有办法暂时隐藏或删除从数据库中填充的组合框中的项目?

我有两个组合框,由同一列(To和From Machine Number)填充

你不能真正将Motor1连接到Motor1,并且不必为每个选择重新填充整个组合框,我认为必须有一种方法可以暂时隐藏第二个组合框中的相同选择。

如果您需要有关组合框如何填充的信息(代码等),请告诉我。

编辑以下是组合框的填充代码:

void PopulateCreateView(CableID_CreateView CView)
{
    // Creates a new Model, and gets data from the Db.
    CModel = new CableID_Model();
    CModel.CNId = 1;
    Database_Facade.Operation_Switch(OPREAD);

    // Populates the form with data for the Plant Area Codes, Supplier Info and Major Equipment.
    foreach (PlantAreaCode_Model Model in PlantAreaCode_Controller.PList)
    {
        CView.cmbAreaCode.Items.Add(Model.AreaName);
        CView.lblDummy.Text = Model.AreaName;
        if (CView.lblDummy.Width > CView.cmbAreaCode.DropDownWidth)
        {
            // Sets the width +20 to allow for the scroll bar.
            CView.cmbAreaCode.DropDownWidth = CView.lblDummy.Width + 20;
        }
    }

    foreach (SupplierID_Model Model in SupplierID_Controller.SList)
    {
        if (Model.CondConfig != null) { CView.cmbXsec.Items.Add(Model.CondConfig); }
        if (Model.Insulation != null) 
        { 
            CView.cmbInsulation.Items.Add(Model.Insulation);
            CView.lblDummy.Text = Model.Insulation;
            if (CView.lblDummy.Width > CView.cmbInsulation.DropDownWidth)
            {
                // Sets the width +20 to allow for the scroll bar.
                CView.cmbInsulation.DropDownWidth = CView.lblDummy.Width + 20;
            }
        }
    }

    foreach (MajorEquipment_Model Model in MajorEquipment_Controller.MeList)
    {
        CView.cmbFromLoc.Items.Add(Model.EqipmentNumber);
        CView.cmbToLoc.Items.Add(Model.EqipmentNumber);
    }
}

以下是MySQL Query的代码:

public void GetCableId(CableID_Model CModel)
{
    DbConnect();

    try
    {
        MajorEquipment_Controller.MeList = new List<MajorEquipment_Model>();
        mySqlCommand = mySqlConnect.CreateCommand();
        mySqlCommand.CommandText = "SELECT * FROM MajorEquipment;";
        mySqlReader = mySqlCommand.ExecuteReader();

        while (mySqlReader.Read())
            {
                MajorEquipment_Controller.MeList.Add(new MajorEquipment_Model
                {
                    EqipmentNumber = Convert.ToString(mySqlReader["EquipmentNumber"])
                });
            }
            mySqlReader.Close();
            mySqlCommand.ExecuteNonQuery();
    }
    catch (MySqlException e) { MessageBox.Show(e.Message); }
    finally
    {
        if (mySqlConnect != null)
        {
            mySqlConnect.Close();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

不幸的是,没有简单方法可以隐藏ListBox / ComboBox项目。

您可以做的是简化您用于将项目加载到列表中的逻辑。由于ComboBox都是必须相互依赖的机器列表


要保留隐藏项目,您必须创建仅从集合中呈现可见项目的自定义控件。

答案 1 :(得分:0)

伪代码解决方案

On DropDownListChange
   Get Selected Item
   for all other drop down lists 
       restore items from backup
       if selected value is not default
          remove current item from list box
相关问题