Link to home
Start Free TrialLog in
Avatar of r_badris
r_badris

asked on

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.
Avatar of peachapol
peachapol

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";
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";
is this combo in win form or asp .net web page
plz specify
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
ASKER CERTIFIED SOLUTION
Avatar of Pigtor
Pigtor
Flag of Mexico image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of r_badris

ASKER

Thanks Buddies will try out yours options!!! :-)