C#: Using one Data Table in order to fill 2 different comboboxes?

Posted by odiseh on Stack Overflow See other posts from Stack Overflow or by odiseh
Published on 2010-05-16T10:53:32Z Indexed on 2010/05/16 11:00 UTC
Read the original article Hit count: 221

Filed under:
|
|
|

Hi all.

I have 2 comboboxes on a form (form1) called combobox1 and combobox2.

Each comboboxes should be filled with data stored in 2 different tables in Sql server 2005: table1 and table2

I mean: combobox1 --> table1 combobox2 --> table2

I fill data table with proper data and then bind the comboboxes to it separately.

My problem is: after filling 2 combos, both of them have equal data got from table2.

This is my code:

        DataTable tb1 = new DataTable();

        //Filling tb1 with data got from table1
        combobox1.Items.Clear();
        combobox1.DataSource = tb1;
        combobox1.DisplayMember = "Name";
        combobox1.ValueMember = "ID";
        combobox1.SelectedIndex = -1;

        //filling tb1 with data got from table2
        combobox2.Items.Clear();
        combobox2.DataSource = tb1;
        combobox2.DisplayMember = "Name";
        combobox2.ValueMember = "ID";
        combobox2.SelectedIndex = -1;

What's wrong?

It seems that if I get 2 different data tables (tb1 and tb2) , every thing will be all right.

Any suggestions please.

Thank you

© Stack Overflow or respective owner

Related posts about c#

Related posts about combobox