Windows Develop Bookmark and Share   
 index > Windows Forms General > Binding combobox2 to combobox1
 

Binding combobox2 to combobox1

Hello Friends,
I have a database in access
I have 2 tables One is Architect(ArchitectID,ArchitectName,ArchitectAddr) and
Client(ClientID,ArchitectID,ClientName)

I have two Comboboxes.
I have to display ArchitectNames in one combobox
And depending upon the selection of architect I want to display the clientNames in the second combobox

How can i do this?
Please provide the code.
sweety1112  Thursday, September 03, 2009 5:45 PM
Hello sweety,

Here is the code that can meet your second requirement. The "dtArchitect" and "dtClient" are created manually in my code in order to test it, you can fill them with SqlDataAdapter accordingly.
public partial class Form1 : Form
{
private DataTable dtArchitect;
private DataTable dtClient;

public Form1()
{
InitializeComponent();
dtArchitect = new DataTable();
dtArchitect.Columns.Add("ArchitectID", typeof(int));
dtArchitect.Columns.Add("ArchitectName", typeof(string));
dtArchitect.Columns.Add("ArchitectAddr", typeof(string));
dtArchitect.Rows.Add(1, "Architect1", "");
dtArchitect.Rows.Add(2, "Architect2", "");
dtArchitect.Rows.Add(3, "Architect3", "");

dtClient = new DataTable();
dtClient.Columns.Add("ClientID", typeof(int));
dtClient.Columns.Add("ArchitectID", typeof(int));
dtClient.Columns.Add("ClientName", typeof(string));
dtClient.Rows.Add(1, 1, "Client1");
dtClient.Rows.Add(2, 2, "Client2");
dtClient.Rows.Add(3, 3, "Client3");

comboBox1.DataSource = dtArchitect;
comboBox1.DisplayMember = "ArchitectName";
comboBox1.ValueMember = "ArchitectID";

listBox1.DataSource = dtClient.DefaultView;
listBox1.DisplayMember = "ClientName";
listBox1.ValueMember = "ClientID";


comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

string archId = comboBox1.SelectedValue.ToString();
string filterString = "ArchitectID=" + archId;
dtClient.DefaultView.RowFilter = filterString;
}

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string archId = comboBox1.SelectedValue.ToString();
string filterString = "ArchitectID=" + archId;
dtClient.DefaultView.RowFilter = filterString;
}
}
In the example, I bound the ListBox1 to dtClient.DefaultView. I can use RowFilter to filter the rows according to the selected ArchitectID in ComboBox1.

If you have anything unclear, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
  • Proposed As Answer byJohnGrove Wednesday, September 09, 2009 3:35 AM
  • Marked As Answer bysweety1112 Wednesday, September 09, 2009 7:13 AM
  •  
Kira Qian  Wednesday, September 09, 2009 2:48 AM
So, let's assume we have a DataTable populated with architects and it is bound to listBox1.

//Bind to ListBox1
listBox1.DataSource = architects;
listBox1.DisplayMember = "ArchitectName";
listBox1.ValueMember = "ArchitectID";
listBox1.SelectedIndexChanged += new Eventhandler(listBox1_SelectedIndexChanged);


With me so far?

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
//if you wanted this row for anything??
DataRowparentRow = (listBox1.DataSource as DataTable).AsEnumerable()
.Where(i => i.Field<Int32>("ArchitectID") == (Int32)listBox1.SelectedValue)
.Select(i => i).Single<DataRow>();

//the rows we want
DataRow[] childRows =client.Select(String.Format("ArchitectID = {0}", (Int32)listBox1.SelectedValue));

}

John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
JohnGrove  Thursday, September 03, 2009 8:23 PM
Hi sweety1112,

Based on my understanding, the tables relationship is Architect.ArchitectID -> Client.ArchitectID. So the relation that can be establish between there to ComboBox is ComboBox1.SelectedValue -> Client.ArchitectID. ComboBox1 is the Architect combobox.

That mean when you select a value in ComboBox2, ComboBox1 will change to a proper value match the Client you have selected. Here is the sample code
public partial class Form1 : Form
{
private DataTable dtArchitect;
private DataTable dtClient;

public Form1()
{
InitializeComponent();
dtArchitect = new DataTable();
dtArchitect.Columns.Add("ArchitectID", typeof(int));
dtArchitect.Columns.Add("ArchitectName", typeof(string));
dtArchitect.Columns.Add("ArchitectAddr", typeof(string));
dtArchitect.Rows.Add(1, "Architect1", "");
dtArchitect.Rows.Add(2, "Architect2", "");
dtArchitect.Rows.Add(3, "Architect3", "");

dtClient = new DataTable();
dtClient.Columns.Add("ClientID", typeof(int));
dtClient.Columns.Add("ArchitectID", typeof(int));
dtClient.Columns.Add("ClientName", typeof(string));
dtClient.Rows.Add(1, 1, "Client1");
dtClient.Rows.Add(2, 2, "Client2");
dtClient.Rows.Add(3, 3, "Client3");

comboBox1.DataSource = dtArchitect;
comboBox1.DisplayMember = "ArchitectName";
comboBox1.ValueMember = "ArchitectID";

comboBox2.DataSource = dtClient;
comboBox2.DisplayMember = "ClientName";
comboBox2.ValueMember = "ClientID";

comboBox1.DataBindings.Add("SelectedValue", dtClient, "ArchitectID");
}
}

So your requirement is not easy to implement with DataBinding.

Please inform me if my suggestion doesn't help you.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Monday, September 07, 2009 3:30 AM
Thank you,

But its not working.
And my requirement is also changed now
I want to populate the database into combobox and depending upon the combobox selection i want to
populate the corresponding details into an listbox.

And another problem is that after inserting data into database (after addrow) the changes doesnot reflect into the table

Thank you,,
sweety1112  Tuesday, September 08, 2009 7:58 AM
Hi sweety1112,

>I want to populate the database into combobox and depending upon the combobox selection
Could you please explain it more specific?

>I want to populate the corresponding details into an listbox.
Do you mean when you load the record according to user selection, these information should be populated to the ListBox?

Since you have changed your design, please give me more information about that. And also if you still keep the previous requirement, you can tell me why my solution isn't work on your side.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Tuesday, September 08, 2009 9:51 AM
Hello,
I have 2 tables
Architect(ArchitectID,ArchitectName,ArchitectAddr) and
Client(ClientID,ArchitectID,ClientName)

One combobox which retrieves the ArchitectName from database

And a listbox which must display all the clients for the corresponding selected architect.
The selection would be based on ArchitectID selected in the combobox.

Thank you,
Regards,
sweety1112  Tuesday, September 08, 2009 2:15 PM
Hello sweety,

Here is the code that can meet your second requirement. The "dtArchitect" and "dtClient" are created manually in my code in order to test it, you can fill them with SqlDataAdapter accordingly.
public partial class Form1 : Form
{
private DataTable dtArchitect;
private DataTable dtClient;

public Form1()
{
InitializeComponent();
dtArchitect = new DataTable();
dtArchitect.Columns.Add("ArchitectID", typeof(int));
dtArchitect.Columns.Add("ArchitectName", typeof(string));
dtArchitect.Columns.Add("ArchitectAddr", typeof(string));
dtArchitect.Rows.Add(1, "Architect1", "");
dtArchitect.Rows.Add(2, "Architect2", "");
dtArchitect.Rows.Add(3, "Architect3", "");

dtClient = new DataTable();
dtClient.Columns.Add("ClientID", typeof(int));
dtClient.Columns.Add("ArchitectID", typeof(int));
dtClient.Columns.Add("ClientName", typeof(string));
dtClient.Rows.Add(1, 1, "Client1");
dtClient.Rows.Add(2, 2, "Client2");
dtClient.Rows.Add(3, 3, "Client3");

comboBox1.DataSource = dtArchitect;
comboBox1.DisplayMember = "ArchitectName";
comboBox1.ValueMember = "ArchitectID";

listBox1.DataSource = dtClient.DefaultView;
listBox1.DisplayMember = "ClientName";
listBox1.ValueMember = "ClientID";


comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);

string archId = comboBox1.SelectedValue.ToString();
string filterString = "ArchitectID=" + archId;
dtClient.DefaultView.RowFilter = filterString;
}

void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string archId = comboBox1.SelectedValue.ToString();
string filterString = "ArchitectID=" + archId;
dtClient.DefaultView.RowFilter = filterString;
}
}
In the example, I bound the ListBox1 to dtClient.DefaultView. I can use RowFilter to filter the rows according to the selected ArchitectID in ComboBox1.

If you have anything unclear, please feel free to tell me.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
  • Proposed As Answer byJohnGrove Wednesday, September 09, 2009 3:35 AM
  • Marked As Answer bysweety1112 Wednesday, September 09, 2009 7:13 AM
  •  
Kira Qian  Wednesday, September 09, 2009 2:48 AM
Hello,
I am very sorry but as i am very new in .net I dont knw how to solve this problem,,,,
Your answer is working but
its giving following error
Cannot find column [System.Data.DataRowView].
comboBox1.SelectedItem.ToString(); it returns an object.

this is what the arch id contains:: ArchitectID = System.Data.DataRowView
sweety1112  Wednesday, September 09, 2009 7:37 AM
Hello sweety,

You can use comboBox1.SelectedValue.ToString() just like the one in my sample. Since you have bind comboBox1 with dtArchitect, its ValueMember has been map to the ArchitectID. So the comboBox1.SelectedValue will return the selected item's ArchitectID in object type.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, September 09, 2009 7:44 AM
Hello,
I tried ToString() but its giving me the same error.
I even tried this which I found some where else

string archID = ((System.Data.DataRowView)comboBox1.SelectedValue).Row.ItemArray[0].ToString();

But its giving same error
I have also set the property SelectedIndex= -1;
sweety1112  Wednesday, September 09, 2009 7:59 AM
Hello sweety,

Why do you always try to convert comboBox1.SelectedValue to System.Data.DataRowView?

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, September 09, 2009 8:02 AM

Hello,I saw it as an answer to this problem
But can you tell me some other solution?

sweety1112  Wednesday, September 09, 2009 8:05 AM
Hello,

>string archID = ((System.Data.DataRowView)comboBox1.SelectedValue).Row.ItemArray[0].ToString();

I don't understand the purpose of this code. I don't know what kind of solution do you want besides this one.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
Kira Qian  Wednesday, September 09, 2009 8:10 AM

You can use google to search for other answers

Custom Search

More Threads

• Funky focus with select and enable
• killing an executable at right time; when the file it creates is stable/full
• Problems scaling flowLayoutPanels
• How can I conslidate multiple pages into 1 print job?
• MDI ToolStripManager Multiple ToolStrips Merge
• Menu in .Net Windows Forms
• Isolines in VB.net app
• Identifing correct latest files from the Internet Temp Folder
• ComboBox Column AutoCompleteMode = AutoCompleteMode.SuggestAppend Width Problem
• windows forms maximize issue