combo control in .NET

Hi all,

The following code is behaving as, when i select combo1 the corresponding value in combo2 is also changed i havent implemented any code for selectionchanged event for any of the combo boxes.

kindly provide me feedback for this problem..is this bug or funtionality........?
DataTable dt = new DataTable();
                  dt.Columns.Add("ID");
                  dt.Columns.Add("NM");
      
                  for (int i=0;i<5; i++)
                  {
                        dr = dt.NewRow();
                        dr[0] = i;
                        dr[1] = "A" + i.ToString();
                        dt.Rows.Add(dr);
                  }
                                    
                  comboBox1.DataSource = dt.DefaultView;      
                  comboBox2.DataSource = dt.DefaultView;
            
                  comboBox1.DisplayMember="ID";
                  comboBox1.ValueMember = "ID";
            
                  comboBox2.DisplayMember="NM";
                  comboBox2.ValueMember = "NM";

Regards,

Badri Narayanan R.
r_badrisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

peachapolCommented:
dt.DefaultView is the referrence type value, so try to create another datatable and you won't see the problem anymore.

example
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("NM");
     
for (int i=0;i<5; i++)
{
    dr = dt.NewRow();
    dr[0] = i;
    dr[1] = "A" + i.ToString();
    dt.Rows.Add(dr);
}

DataTable dt_2 = new DataTable();
dt_2.Columns.Add("ID");
dt_2.Columns.Add("NM");
     
for (int i=0;i<5; i++)
{
    dr = dt_2.NewRow();
    dr[0] = i;
    dr[1] = "A" + i.ToString();
    dt_.2Rows.Add(dr);
}


comboBox1.DataSource = dt.DefaultView;    
comboBox2.DataSource = dt_2.DefaultView;
         
comboBox1.DisplayMember="ID";
 comboBox1.ValueMember = "ID";
         
comboBox2.DisplayMember="NM";
comboBox2.ValueMember = "NM";
0
peachapolCommented:
in order to make your code shorter, you can use DataTable.Copy.

DataTable dt = new DataTable();
DataTable dt_2 = new DataTable();

dt.Columns.Add("ID");
dt.Columns.Add("NM");
     
for (int i=0;i<5; i++)
{
    dr = dt.NewRow();
    dr[0] = i;
    dr[1] = "A" + i.ToString();
    dt.Rows.Add(dr);
}

dt_2 = dt.Copy();

comboBox1.DataSource = dt.DefaultView;    
comboBox2.DataSource = dt_2.DefaultView;
         
comboBox1.DisplayMember="ID";
 comboBox1.ValueMember = "ID";
         
comboBox2.DisplayMember="NM";
comboBox2.ValueMember = "NM";
0
gemailjCommented:
is this combo in win form or asp .net web page
plz specify
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Arthur_WoodCommented:
to explain what is happening, you have assigned the SAME dataTable to BOTH comboxes.  So when the DataTable entry you are pointing to changes in Combo Box1, the same entry in Combox Box 2 will be selected, since it is the same reference in the same DataTable.

If you want the two to act independently, then you will need two, completely independent Data Tables, as suggested by peachapol.

AW
0
PigtorCommented:
The reason both controls change at the same time is because the controls are connected to the same DataTable in the same BindingContext.
You should create a new BindingContext for each ComboBox to prevent this behavior.

For example:

comboBox1.DataSource = dt.DefaultView;
comboBox1.ValueMember = "ID";
comboBox1.DisplayMember = "ID";

comboBox2.BindingContext = new BindingContext();
comboBox2.DataSource =dt.DefaultView;
comboBox2.ValueMember = "NM";
comboBox2.DisplayMember = "NM";


You can find an example at: http://www.akadia.com/services/dotnet_unshare_datasource.html
More information about BindingContext at:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformsbindingcontextclasstopic.asp

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
r_badrisAuthor Commented:
Thanks Buddies will try out yours options!!! :-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.