multiple comboboxes binded with same dataset
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
Re: multiple comboboxes binded with same dataset
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.
Re: multiple comboboxes binded with same dataset
better you use different datatable or dataset for your combobox.
Re: multiple comboboxes binded with same dataset
but when i have multiple combo boxes what should be the approach?
please help me in this regard
Thanks and Regards
Vinay kumar
Re: multiple comboboxes binded with same dataset
Quote:
Originally Posted by RaviIntegra
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.
Quote:
Originally Posted by kumar_1981
but when i have multiple combo boxes what should be the approach?
please help me in this regard
Thanks and Regards
Vinay kumar
Am 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:
vb.net Code:
BindingSource1.DataSource = myDataTable
BindingSource2.DataSource = myDataTable
ComboBox1.DataSource = BindingSource1
ComboBox2.DataSource = BindingSource2
Obviously you need to substitute the names of your actual variables.
Re: multiple comboboxes binded with same dataset
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
Re: multiple comboboxes binded with same dataset
Of course you are. You're creating two BindingSources then binding both ComboBoxes to the same one:
C# Code:
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:
C# Code:
comboBox1.DisplayMember = "users.UserName".Trim();
What exactly is being trimmed?
Re: multiple comboboxes binded with same dataset
sorry for the mistake with databind i did
Thank u so much for being patient with my mistakes
Thanks once again
Regards
Vinay Kumar