zeencat
zeencat

Reputation: 590

How to populate a ComboBox After adding new item

I'm developing a SSIS control flow task. On my UI I have a combobox which displays a list of avaialble user variables as well as the ability add a new variable. I'm able to add the variable successfully but I can't see it in my combobox even after I repopulate the datasource. On the SelectionChangeCommitted event I am saving the combobox selection. If the user selects to add a new SSIS user variable then they are prompted to within this method. The variable is saved and then I am repopulating the datasource of the combobox. Although my new variable isn't being shown in the drop down list. Should I be repopulating the combobox on a different event?

An example of the code I am using below.

  private List<string> FillVariablesList()
    {
        List<string> Variables = new List<string>();
        Variables.Add("");
        Variables.Add(New_Variable);
        foreach (Variable v in this.theTaskHost.Variables)
        {
            if (!v.SystemVariable && v.DataType == TypeCode.String)
                Variables.Add(v.Name);
        }

        return Variables;
    }

 combobox.datasource = FillVariablesList();

Upvotes: 0

Views: 427

Answers (1)

Mike Perrenoud
Mike Perrenoud

Reputation: 67898

Try using a shared ObservableCollection like this.

... 
class A
{
    private ObservableCollection<string> variables = new ObservableCollection<string>();

    ...
    private void FillVariablesList() 
    {
        variables.Clear();
        variables.Add(""); 
        variables.Add(New_Variable); 

        foreach (Variable v in this.theTaskHost.Variables) 
        { 
            if (!v.SystemVariable && v.DataType == TypeCode.String) 
                variables.Add(v.Name); 
        }

        this.comboBox.DataSource = null;
        this.comboBox.DataSource = variables;
    }
}

And you can even setup the FillVariables method so that it just adds the new one rather than clearing it and re-filling - I just don't know how the rest of your code is structured.

Upvotes: 1

Related Questions