c# - How to Match two comboboxes items together -


I have two combobox, which should have two different information.

1.cb1: Select the table_name from Info_schema.tables (this displays several tables)
2.cb2: It should fill with a column name.

Example: I have three tables in CB1 with the same merit but have different values ​​in the column EmpName (tblLondon, tblBerlin, tblRom, ...)

Now when I first select a table in the combo box, then I want to dynamically display in the second cobboxen column Empnem.

  CB1 [TBLDON] CB2 [John, Mewis, Chris, Mike ..]  

OR

  CB1 [TBL Berlin] CB2 [Gunter, Peter, Sophie, Sunny, ..]  

Can I help me

  string c = Configuration Manager. Connection strings [""] connection string; SqlConnection con = new SqlConnection (C); SqlCommand CMD = new SQL Commands (); Cmd.Connection = con; Cmd.CommandText = ("TABLE_NAME select information from SQUEMA.TABLES command by TABLE_NAME ASC"); {// Try Open Connection, save results in DT and execute SPPCR and APDX. Fill it in DT con.Open (); SqlDataAdapter Adapter = New SqlDataAdapter (CMD); Dt = new datatable (); Adapter.fill (dt); CbTbl.DisplayMember = "TABLE_NAME"; CbTbl.ValueMember = "TABLE_NAME"; // DT cbTbl.DataSource = Fill Cambodia with data in dt; // below bzw. Clean Cambodia CBT. Selected index = -1;  

This code is working and my CB1 (popboxing) is populating

And now I do not really know how to go with cb2 < / P>

  Private zero comboBox2_SelectedIndexChanged (Object Sender, EventArgs E) {}  

If I understand you correctly, and if you have this data in SQL Server or another database, then you should use the SelectedIndexChange event and the load item for that code (match) For id Display member and value member use).

See it.

Edit:
You can do this with the following code: (If you do not use the database)
You should use this code Cb1.SelectedIndexChange (or value)

  var cb2items = new dictionary & lt; Int, string & gt; {{1, "name"}, {1, "alias"}, {2, "name"}, {2, "second name"}}; // Use the number for parent ID in cb1 foreach (var item in cb2items) {if (item.Key == int.Parse (comboBox1.SelectedValue.ToString ())} {comboBox2.Items.Add (item); }}  

Edit 2:

Use this code in cb1. Selected Value Change:

  String C = Configuration Manager. Connection connection [""]. Connection string; SqlConnection con = new SqlConnection (C); SqlCommand CMD = new SQL Commands (); Cmd.Connection = con; Cmd.CommandText = ("Choose ... where the name of the tab = CB.1 is the selected value"); SPPCR & amp; Fill it in DT con.Open (); SqlDataAdapter Adapter = New SqlDataAdapter (CMD); Dt = new datatable (); Adapter.fill (dt); CbTbl.DisplayMember = "TABLE_NAME"; CbTbl.ValueMember = "TABLE_NAME"; CbTb2.DataSource = dt;  

And you can also create a process that sends the item back to the table name as input. If you use the process, then your code performs better.

Edit 3:

Press this code in cbTb2. Selected Value Change Event:

  Try {int a = int.Parse (cbTB2.SelectedValue.ToString ()); } Hold {}  

Comments