Hi RAOOFH,
If the relation between two tables are of type master/detail, you can bind the main table to a ComboBox and bind the relation of the tables to another ComboBox. You can follow the document below which did not show the answer directly but can provide you so many ideas:
http://msdn.microsoft.com/en-us/library/aa984462(VS.71).aspx
You can also create two BindingSources the data sources of which are the two tables and bind each to a ComboBox. Then you can handle the SelectedIndexChanged event of one ComboBox. In the handler, you need to get the selected value and take it as a parameter to call the Find method of the BindingSource of the second ComboBox. When find one record, you can set the Position of the BindingSource to synchronize the two ComboBoxes. This is a code snippet:
private DataTable table1;
private DataTable table2;
private BindingSource bindSrc1;
private BindingSource bindSrc2;
private void Form5_Load(object sender, EventArgs e)
{
//Fill table11 and table2.
table1 = GetTable1();
table2 = GetTable2();
//Create and bind the BindingSources.
bindSrc1 = new BindingSource();
bindSrc1.DataSource = table1;
this.comboBox1.DataSource = bindSrc1;
bindSrc2 = new BindingSource();
bindSrc2.DataSource = table2;
this.comboBox2.DataSource = bindSrc2;
//Handle this event to synchronize the selected item.
this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex != -1)
{
DataRowView row = bindSrc1.Current as DataRowView;
//Find the related item and modify the current item of bindingsource2.
int index = bindSrc2.Find("col1",row["col1"]);
if (index != -1)
{
bindSrc2.Position = index;
}
}
}
Let me know if this does not help.
Aland Li
Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.