PDA

Click to See Complete Forum and Search --> : multiple comboboxes binded with same dataset


kumar_1981
Jul 31st, 2007, 02:36 AM
Hi

i am having 2 combo boxes binded to same dataset and different columns.but changing in one combo is getting changed with another combo too


objCon = new SqlConnection("server=server;database=Master;uid=sa;pwd=123@esn;");
objAdapter = new SqlDataAdapter("select * from users", objCon);
ds = new DataSet();
objAdapter.TableMappings.Add("Table", "Users");
objAdapter.TableMappings.Add("Tables", "Users");
objAdapter.Fill(ds);
comboBox1.DataSource = ds;
comboBox1.DisplayMember = "users.Role".Trim();
comboBox2.DataSource = ds;
comboBox2.DisplayMember = "users.UserName".Trim();



its the code i implemented. But changing Role in combo1 results in change in combo2


i used c# 2005 windows application


Thanks and Regards
Vinay Kumar

jmcilhinney
Jul 31st, 2007, 04:11 AM
Create two BindingSources and bind each to a different ComboBox, then bind the DataTable to both BindingSources. Voila! You should always use a BindingSource when data-binding. It provides numerous advantages.

RaviIntegra
Jul 31st, 2007, 04:26 AM
better you use different datatable or dataset for your combobox.

kumar_1981
Jul 31st, 2007, 04:32 AM
but when i have multiple combo boxes what should be the approach?

please help me in this regard


Thanks and Regards
Vinay kumar

jmcilhinney
Jul 31st, 2007, 07:36 AM
better you use different datatable or dataset for your combobox.That's just not true. Why would you create more than one copy of the same data? Then if you add a row you'd have to add it to both tables, delete rows from both tables, etc.but when i have multiple combo boxes what should be the approach?

please help me in this regard


Thanks and Regards
Vinay kumarAm I talking to myself here? I already told you exactly what to do. Let me spell it out:

1. Add your two ComboBoxes to the form.
2. Add two BindingSources to the form.
3. Set the DisplayMember and ValueMember properties of each ComboBox to the names of the appropriate columns, e.g. "Name" for the DisplayMembers and "ID" for the ValueMember.
3. Add the following code after you retrieve the data from the database:BindingSource1.DataSource = myDataTable
BindingSource2.DataSource = myDataTable
ComboBox1.DataSource = BindingSource1
ComboBox2.DataSource = BindingSource2Obviously you need to substitute the names of your actual variables.

kumar_1981
Jul 31st, 2007, 08:14 AM
objCon = new SqlConnection("server=ESNDBSRV;database=Master;uid=sa;pwd=123@esn;");
objAdapter = new SqlDataAdapter("select * from users", objCon);
ds = new DataSet();
objAdapter.Fill(ds);
ds.Tables[0].TableName="users";

btnSource.DataSource = ds;
btnSource2.DataSource = ds;
comboBox1.DataSource = btnSource;
comboBox2.DataSource = btnSource;
comboBox1.DisplayMember = "users.UserName".Trim();
comboBox1.ValueMember = "users.LoginId";
comboBox2.DisplayMember = "users.Role".Trim();
comboBox2.ValueMember = "users.LoginId";

i tried with following code

still i am getting the same problem


Thanks and Regards
Vinay Kumar

jmcilhinney
Jul 31st, 2007, 08:26 AM
Of course you are. You're creating two BindingSources then binding both ComboBoxes to the same one:comboBox1.DataSource = btnSource;
comboBox2.DataSource = btnSource;If you give your variables descriptive names then things like that are less likely to happen. Names like comboBox1 and comboBox2 are all but meaningless. If you name the controls after there purpose and then give the BindingSources names that match the control they're for, then it's easy to see what goes where.

Also, there's no point trimming a literal string that you know has no whitespace:comboBox1.DisplayMember = "users.UserName".Trim();What exactly is being trimmed?

kumar_1981
Jul 31st, 2007, 08:29 AM
sorry for the mistake with databind i did


Thank u so much for being patient with my mistakes


Thanks once again

Regards
Vinay Kumar