Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

combo control in .NET

Posted on 2006-04-17
6
Medium Priority
?
271 Views
Last Modified: 2010-04-17
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.
0
Comment
Question by:r_badris
6 Comments
 

Expert Comment

by:peachapol
ID: 16468135
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
 

Expert Comment

by:peachapol
ID: 16468177
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
 
LVL 5

Expert Comment

by:gemailj
ID: 16468394
is this combo in win form or asp .net web page
plz specify
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 16468913
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
 
LVL 3

Accepted Solution

by:
Pigtor earned 375 total points
ID: 16469437
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
 

Author Comment

by:r_badris
ID: 16475218
Thanks Buddies will try out yours options!!! :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Starting up a Project

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question