Results 1 to 8 of 8

Thread: multiple comboboxes binded with same dataset

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Registered User RaviIntegra's Avatar
    Join Date
    Mar 2007
    Location
    Pondicherry, India
    Posts
    125

    Re: multiple comboboxes binded with same dataset

    better you use different datatable or dataset for your combobox.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    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

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. BindingSource1.DataSource = myDataTable
    2. BindingSource2.DataSource = myDataTable
    3. ComboBox1.DataSource = BindingSource1
    4. ComboBox2.DataSource = BindingSource2
    Obviously you need to substitute the names of your actual variables.
    Last edited by jmcilhinney; Jul 31st, 2007 at 07:42 AM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    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

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. comboBox1.DataSource = btnSource;
    2. 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:
    1. comboBox1.DisplayMember = "users.UserName".Trim();
    What exactly is being trimmed?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Dec 2006
    Posts
    91

    Resolved 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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width