Binding CompboBox after form has loaded

I have a situation whereby my form loads and populates comboBox1 which works fine. However, when the user selects a value from comboBox1 I want to populate comboBox2 with values based on the selection but can't seem to get it to work:

Here is my event handler for the first comboBox.


private void comboBoxTargetProduct_SelectedIndexChanged(object sender, System.EventArgs e)
{
string product = comboBoxTargetProduct.SelectedText.ToString();

switch(product)
{
case "Product1":
{
comboBoxExtensionType.DataSource = Enum.GetNames(typeof(CreativeNRG.ProductType));
this.Refresh();
break;
}
}
}


If it was a web based form then I would simply call the DataBind method but this does not appear to be available in the windows forms environment.

Thanks in advance for your help.

Simon


Answer this question

Binding CompboBox after form has loaded

  • Steven Hemingray - MSFT

    what error/s do you recieve  if there is....
  • Allighator

    ahh Binding to an array.

    to triggerit maybe you need to set datasource to nothing and then to this array.

    Try that.

  • Drew Speedie

    It refreshes the internals, personally I wouldn't have done it like that but that's what I have found to work. Never heard an exact reason why but I suspect that setting it to null resets something inside.


  • dehran ph

    Doesn't look like you are setting a display member. 

    Are you sure there are values getting populated


  • bewA

    Excellent - that worked a treat - thanks.
    Just so I know, can you explain why I need to set the datasource to null first 

    Thanks again.

    Simon

  • mjcs100

    If I simply add the following to the form load method, the control loads and populates itself without a problem, without setting a display member:

    comboBoxExtensionType.DataSource = Enum.GetNames(typeof(CreativeNRG.ProductType));

    However, I know little about what this display member property does, what value would I set it to

    Thanks for your help.

    Simon

  • dr.thomas

    No error just nothing happens !
  • Binding CompboBox after form has loaded