Apr 12th, 2008, 02:15 AM
Master/Detail (Parent/Child) Data-binding (.NET 2.0+ WinForms)
How do you filter child data in one control based on a selection from parent data in another control? Like this:
1. Create a new Windows Forms Application project.
2. Add two ComboBoxes to the form.
3. Add two BindingSources to the form.
4. Add the following code:
Now run the project and make selections from the parent list in the first ComboBox. Watch the child list filter automatically as you do.
private void Form1_Load(object sender, EventArgs e)
// Get the data. The DataSet must contain a Parent table,
// a Child table and a ParentChild relation between them.
DataSet data = this.GetDataSet();
// Bind the parent source to the parent table.
this.bindingSource1.DataSource = data;
this.bindingSource1.DataMember = "Parent";
// Bind the child source to the relationship.
this.bindingSource2.DataSource = this.bindingSource1;
this.bindingSource2.DataMember = "ParentChild";
// Bind the parent control to the parent source.
this.comboBox1.DisplayMember = "Name";
this.comboBox1.ValueMember = "ID";
this.comboBox1.DataSource = this.bindingSource1;
// Bind the child control to the child source.
this.comboBox2.DisplayMember = "Name";
this.comboBox2.ValueMember = "ID";
this.comboBox2.DataSource = this.bindingSource2;
private DataSet GetDataSet()
DataSet data = new DataSet();
DataTable parent = this.GetParentTable();
DataTable child = this.GetChildTable();
// Add a relationship between the ID of the parent
// table and the ParentID of the child table.
private DataTable GetParentTable()
DataTable table = new DataTable("Parent");
private DataTable GetChildTable()
DataTable table = new DataTable("Child");
table.Rows.Add(1, 1, "Child1");
table.Rows.Add(2, 1, "Child2");
table.Rows.Add(3, 1, "Child3");
table.Rows.Add(4, 2, "Child4");
table.Rows.Add(5, 2, "Child5");
table.Rows.Add(6, 2, "Child6");
table.Rows.Add(7, 3, "Child7");
table.Rows.Add(8, 3, "Child8");
table.Rows.Add(9, 3, "Child9");
You can use DataGridView controls instead of ComboBoxes if you like. In fact, because the work is done by the BindingSource components, you can use any controls at all that you can bind to a BindingSource.
Now, before anyone says that that's all well and good but I'm creating the DataTables myself and they are getting their data from a database, that makes absolutely no difference whatsoever. Note that in my Load event handler I call GetDataSet to get a DataSet. All that matters at that point is that a DataSet with the appropriate format is returned. Where that DataSet comes from is irrelevant to the code that uses it, so you can implement the GetDataSet function in any way you like. Obviously, if your tables and relation have different names then you should adjust my code to use those names. I feel that this disclaimer is required because it happens with monotonous regularity that I post example code an someone just copies and pastes it and then wonders why it doesn't work. This is an example only to illustrate a principle. If you want to implement that principle then you can either write your own code from scratch or else use mine as a starting point and adjust it as needed.
Nov 14th, 2008, 12:22 PM
Re: Master/Detail (Parent/Child) Data-binding (.NET 2.0+ WinForms)
This site is really informative!!!!!!!!!
Thanx a lot!
Click Here to Expand Forum to Full Width
Survey posted by VBForums.