Link to home
Start Free TrialLog in
Avatar of mrong
mrong

asked on

Dynamic Combo Box

Greetings to everyone,

I have 3 Combo Boxes on a C# form. All the data for those 3 ComboBox are popullated from an Oracle table. I want these3 ComboBox are dynamic linked to each other. That means, if I select another item in ComboBox1, the item in ComboBox2 will be changes to those associates with ComboBox1. Same to ComboBox3 and it depends on the item on ComboBox2.

I use the following code to fill ComboBox1.

      foreach (DataRow myRow in MyDataSet3.Tables[0].Rows)
                  {
                        campus.Items.Add(myRow["Region_code"]);
                  }

I know I need to modified the code for ComboBox1.TextChange. But need more details info.
Thanks in advance.

-Mark
Avatar of mrong
mrong

ASKER

Please ignore this question. I have found the solution by myself. I will post it by next Monday.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of TheAvenger
TheAvenger
Flag of Switzerland 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 mrong

ASKER

Hi TheAvenger,

Thank you anyway:)

Mark
Avatar of mrong

ASKER

All you need to do is modified the code for SelectedIndexChanged. Here is my code, but you might find other better way to do it.

private void zone_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  bldg.Items.Clear();
                  
                  string sql4;
                  DataSet MyDataSet5 = new DataSet();  
                  sql4 = "SELECT DISTINCT BLDG";
                  sql4 += " FROM ROOT.AE_S_BLD_C";
                  sql4 += " WHERE REGION_CODE =('"+campus.Text+"') AND FAC_ID=('"+zone.Text+"')";
            sql4 += " ORDER BY BLDG";

                  OleDbConnection myConn4 = oleDbConnection1;  
                  OleDbCommand myComm4 = new OleDbCommand();
                  myComm4.Connection = myConn4;
                  myComm4.CommandText = sql4;

                  OleDbDataAdapter myAdap4 = new OleDbDataAdapter(myComm4);
                  myAdap4.Fill(MyDataSet5);
           
                  
                  
                  foreach (DataRow myRow in MyDataSet5.Tables[0].Rows)
                  {
                  
                        bldg.Items.Add(myRow["BLDG"]);

                  }

                  
                  myConn4.Close();
            }
The way I showed you is much more '.NET like' and is also much more efficient if I start playing with the combo boxes. It also needs only one connection to the database
I have supplied an answer that solves the problem the moment when mrong decided to stop it. I have also explained what is the added value and advantage of my solution over the one he presented. On my opinion the question should not be closed with PAQing as a good solution that can be used by the author was supplied.
Avatar of mrong

ASKER

I haven't got a chance to try TheAvenger's solution and I am working on something else. Please give me a couple of days to do so. I have no problem for giving points to the people who provide the best solution.

Thanks.