Question

I was wondering if there was a way to temporarily hide or remove an item from a combo box that is being populated from a database?

I have two combo boxes that are being populated by the same column (To and From Machine Number)

You can't really have Motor1 connected to Motor1, and short of having to repopulate an entire combo box with each selection, I was thinking there has to be a way to temporarily hide the same selection from the second combobox.

Please let me know if you need information on how the comboboxes are being populated(code etc.)

EDIT Here is the population code for the combo boxes:

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

Here is the code for the 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();
        }
    }
}

No correct solution

OTHER TIPS

Unfortunately there is no simple way to keep a ListBox / ComboBox item hidden.

What you can do is simplfy the logic you are using to Load item onto the List. Since both the ComboBox are list of machines that are bound to be dependent on each other


To keep a hidden item you have to create your custom controls that only renders the visible item from collection.

A pseudocode solution would be

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top