0
votes
class ComboBoxCompany
{
    public string Code;
    public string Name;
    public string Database;

    public ComboBoxCompany(string code, string name, string database)
    {
        Code = code;  Name = name; Database = database;
    }

    public override string ToString()
    {
        // Generates the text shown in the combo box
        return Name;
    }
}

class ComboBoxDatabase
{
    public string cmpName;
    public string dbName;

    public ComboBoxDatabase(string cmpname, string dbname)
    {
        cmpName = cmpname; dbName = dbname;
    }

    public override string ToString()
    {
        // Generates the text shown in the combo box
        return cmpName + " - " + dbName;
    }
}

these are the classes for the 2 comboboxes, so when i select a value of the first one(ComboBoxCompany), i want that the second combobox(ComboBoxDatabase) chooses the "dbName"-Value from the first combobox "Database"-Value

i tried this, but it doesn't

    private void cbxBranch_SelectedIndexChanged(object sender, EventArgs e)
    {
        cbxDatabase.SelectedItem = (cbxCompany.SelectedItem as ComboBoxCompany).Database;
    }
1

1 Answers

0
votes

by setting (cbxCompany.SelectedItem as ComboBoxCompany).Database You try to set the selected item to the database string which does not 'exist' because it seems you added a class as comboboxitem.

You need to set the SelectedItem to the real object. You could search for this by using linq (sample code):

var company = cbxCompany.SelectedItem as ComboBoxCompany;
if(company == null)
   return;
var dbItem = _databaseComboBoxItems.FirstOrDefault(x=>x.CompanyName = company.CompanyName && x.Database == company.DatabaseName);
if(dbItem == null)
  return;
 cbxDatabase.SelectedItem = dbItem;