locked
Data binding in Win Forms RRS feed

  • Question

  • Seems like a really easy question, but I'll ask it anyway: I drive the population of one combo box based on the selected value of another combo box all the time in ASP.NET at design time by binding the first's required parameter to the second control. I'm trying to do the same thing in Win Forms but cannot seem to find out how to do it. Like I said, probably WinForms 101 but I'm new to them, so thanks for any help you can provide ...
    Wednesday, August 5, 2009 5:23 AM

Answers

  • Hi bobness,

     

    We can create a BindingSource and bind it to comboBox2 whose data source based on the selected value of comboBox1. This is a code snippet:
    BindingSource bindSrc = new BindingSource();

    bindSrc.DataSource = GetComboBox2DataSource();

    comboBox2.DataSource = bindSrc;

     

    Then we need to handle the SelectedValueChanged event to filter the data source of comboBox2. This is a code snippet:
    const string FILTER_COL_NAME = "col1";

    void comboBox1_SelectedValueChanged(object sender, EventArgs e)

    {

        BindingSource bindSrc = comboBox2.DataSource as BindingSource;

        bindSrc.Filter = string.Format("{0} = '{1}'",       FILTER_COL_NAME,comboBox1.SelectedValue);

    }

     

    This is a sample shows how to bind data to a ComboBox:
    http://msdn.microsoft.com/en-us/library/x8ybe6s2(VS.80).aspx.

     

    Let me know if this helps.
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    • Marked as answer by Aland Li Thursday, August 13, 2009 3:55 AM
    Thursday, August 6, 2009 7:40 AM