I need to create two combo box and when i select a value in combo box 1, a related value should display in combo box 2. the Values are taken from two different database table (deviceCat and Device). Please i need help on this for my C# winform application. I am a newbie to c# Programming. thanks.
7 Answers
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("weekdays");
comboBox1.Items.Add("year");
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Items.Clear();
if (comboBox1.SelectedItem == "weekdays")
{
comboBox2.Items.Add("Sunday");
comboBox2.Items.Add("Monday");
comboBox2.Items.Add("Tuesday");
}
else if (comboBox1.SelectedItem == "year")
{
comboBox2.Items.Add("2012");
comboBox2.Items.Add("2013");
comboBox2.Items.Add("2014");
}
}
}
}

- 88
- 12
-
I want both to pull data from two database table – Umar E. Shaibu Mar 06 '15 at 11:48
-
You can use this link [link](http://stackoverflow.com/questions/25216492/how-to-retrieve-tables-of-a-database-in-a-combobox-using-an-windows-form-applica) and this [link](http://stackoverflow.com/questions/27597250/how-bind-database-values-to-combo-box-in-windows-appliation) and this [link](http://stackoverflow.com/questions/7423911/how-to-populate-c-sharp-windows-forms-combobox) – Alireza Hosseinitabar Mar 06 '15 at 12:00
you can use the OnSelectedValueChanged
event handler to trigger the code to clear and re populate the data in the second combobox

- 4,699
- 7
- 17
- 26

- 1,261
- 1
- 14
- 25
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.Text)
{
case "Something":
comboBox2.Items.Clear();
comboBox2.Items.Add("Option1");
comboBox2.Items.Add("Option2");
comboBox2.Items.Add("Option3");
break;
case "Something else":
comboBox2.Items.Clear();
comboBox2.Items.Add("Option4");
comboBox2.Items.Add("Option5");
comboBox2.Items.Add("Option6");
break;
default:
break;
}
}
If you do something like this it should work. What this does is it checks the text in the combobox1
and then modifies the text of the second one accordingly

- 205
- 3
- 20
Just a quick mockup:
var cb1 = new ComboBox();
var cb2 = new ComboBox();
cb1.Items.AddRange(firstItems); // whatever you want to put into the first ComboBox
cb2.Items.AddRange(secondItems); // whatever you want to put into the second ComboBox
public void ChangeSelectionInSecondComboBox(object sender, EventArgs e)
{
var selectedItem = cb1.SelectedItem;
if(selectedItem == [...]) // depends on how you want to decide which item
to display in ComboBox2
{
cb2.SelectedIndex = [...] // you need to decide which Index to set
}
}
cb1.SelectedIndexChanged += ChangeSelectionInSecondComboBox;
Essentially, what you want is to listen to the SelectionChanged
event of your first ComboBox
and then select the relevant item in the second ComboBox
.

- 4,628
- 1
- 21
- 38
You simply need to take advantage of the selectIndexChanged event of your master combobox to update the selectedItem from your dependant combobox.
Here is an illustration. I have two classes. A client class and a Contract class. Every client has a contract
Here is the Contract class:
public class Contract
{
public String ContractNumber { get; set; }
public Contract(String contractNumber)
{
ContractNumber = contractNumber;
}
public override string ToString()
{
return "Contract Number :" + ContractNumber;
}
}
And here is the Client class:
public class Client
{
public String Name { get; set; }
public Contract SignedContract { get; set; }
public Client(String name, Contract contract)
{
Name = name;
SignedContract = contract;
}
public override string ToString()
{
return "Client " + Name;
}
}
A few sample datas to make a demo work (called in the formLoad event of the windows form)
Contract c1 = new Contract("NRCONTRACT0001");
Contract c2 = new Contract("NRCONTRACT0005");
Contract c3 = new Contract("NCAB_84_PK65");
Client cl1 = new Client("BTHOS", c1);
Client cl2 = new Client("FTIA Consulting", c2);
Client cl3 = new Client("GAMMA INFO", c3);
Client[] clients = new Client[] {cl1,cl2,cl3};
Contract[] contracts = new Contract[] {c1, c2, c3};
comboBox1.Items.AddRange(clients);
comboBox2.Items.AddRange(contracts);
Then in the first combox box select index changed event, I pick up the object and cast it into a Client in order to take advantage of the relation between client and contract
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.SelectedItem = ((Client)comboBox1.SelectedItem).SignedContract;
}
It works fine.

- 4,414
- 4
- 32
- 41
private DataSet m_ds = new DataSet();
private CurrencyManager m_Currency;
private void Form8_Load(object sender, EventArgs e)
{
DataTable T1 = m_ds.Tables.Add("T1");
T1.Columns.Add("T");
T1.Columns.Add("V");
DataRow T1_ROW = T1.NewRow();
T1_ROW["T"] = "1";
T1_ROW["V"] = "1";
T1.Rows.Add(T1_ROW);
T1_ROW = T1.NewRow();
T1_ROW["T"] = "2";
T1_ROW["V"] = "2";
T1.Rows.Add(T1_ROW);
T1_ROW = T1.NewRow();
T1_ROW["T"] = "3";
T1_ROW["V"] = "3";
T1.Rows.Add(T1_ROW);
T1_ROW = T1.NewRow();
T1_ROW["T"] = "4";
T1_ROW["V"] = "4";
T1.Rows.Add(T1_ROW);
comboBox1.DataSource = T1;
comboBox1.DisplayMember = "T";
comboBox1.ValueMember = "V";
m_Currency = this.BindingContext[m_ds, "T1"] as CurrencyManager;
DataTable T2 = m_ds.Tables.Add("T2");
T2.Columns.Add("ID");
T2.Columns.Add("T");
T2.Columns.Add("V");
DataRow T2_ROW = T2.NewRow();
T2_ROW["ID"] = "1";
T2_ROW["T"] = "1-1";
T2_ROW["V"] = "1-1";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "1";
T2_ROW["T"] = "1-2";
T2_ROW["V"] = "1-2";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "1";
T2_ROW["T"] = "1-3";
T2_ROW["V"] = "1-3";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "1";
T2_ROW["T"] = "1-4";
T2_ROW["V"] = "1-4";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "2";
T2_ROW["T"] = "2-1";
T2_ROW["V"] = "2-1";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "2";
T2_ROW["T"] = "2-2";
T2_ROW["V"] = "2-2";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "2";
T2_ROW["T"] = "2-3";
T2_ROW["V"] = "2-3";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "2";
T2_ROW["T"] = "2-4";
T2_ROW["V"] = "2-4";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "3";
T2_ROW["T"] = "3-1";
T2_ROW["V"] = "3-1";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "3";
T2_ROW["T"] = "3-2";
T2_ROW["V"] = "3-2";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "3";
T2_ROW["T"] = "3-3";
T2_ROW["V"] = "3-3";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "3";
T2_ROW["T"] = "3-4";
T2_ROW["V"] = "3-4";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "4";
T2_ROW["T"] = "4-1";
T2_ROW["V"] = "4-1";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "4";
T2_ROW["T"] = "4-2";
T2_ROW["V"] = "4-2";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "4";
T2_ROW["T"] = "4-3";
T2_ROW["V"] = "4-3";
T2.Rows.Add(T2_ROW);
T2_ROW = T2.NewRow();
T2_ROW["ID"] = "4";
T2_ROW["T"] = "4-4";
T2_ROW["V"] = "4-4";
T2.Rows.Add(T2_ROW);
System.Data.DataColumn T1_V = null;
System.Data.DataColumn T2_V = null;
T1_V = m_ds.Tables["T1"].Columns["V"];
T2_V = m_ds.Tables["T2"].Columns["ID"];
System.Data.DataRelation REL = new DataRelation("COM_ID", T1_V, T2_V);
m_ds.Relations.Add(REL);
comboBox2.DataSource = m_ds;
comboBox2.DisplayMember = "T1.COM_ID.T";
comboBox2.DisplayMember = "T1.COM_ID.V";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DataRow[] Rows = m_ds.Tables["T1"].Select("V = '" + Convert.ToString(comboBox1.SelectedValue) + "'");
if (Rows.Count() < 1)
return;
m_Currency.Position = m_ds.Tables["T1"].Rows.IndexOf(Rows[0]);
}

- 1
- 1
-
It is first combobox selectedindex change event, second change currency manager position, therd chage relation member in table, final chage second combobx list – dongsik um Nov 09 '16 at 04:58
Shaibu , I know too late for it but it maybe will help someone
just add this line code cmbDevice1.Items.Clear();
before this line cmbDevice1.Items.Add(myReader2[""]);

- 4,699
- 7
- 17
- 26

- 3
- 4