# VBForums CodeBank > CodeBank - C# >  Master/Detail (Parent/Child) Data-binding (.NET 2.0+ WinForms)

## jmcilhinney

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:

```
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();

    data.Tables.Add(parent);
    data.Tables.Add(child);

    // Add a relationship between the ID of the parent
    // table and the ParentID of the child table.
    data.Relations.Add("ParentChild",
                       parent.Columns["ID"],
                       child.Columns["ParentID"]);

    return data;
}

private DataTable GetParentTable()
{
    DataTable table = new DataTable("Parent");

    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    table.Rows.Add(1, "Parent1");
    table.Rows.Add(2, "Parent2");
    table.Rows.Add(3, "Parent3");

    return table;
}

private DataTable GetChildTable()
{
    DataTable table = new DataTable("Child");

    table.Columns.Add("ID", typeof(int));
    table.Columns.Add("ParentID", typeof(int));
    table.Columns.Add("Name", typeof(string));

    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");

    return table;
}
```

Now run the project and make selections from the parent list in the first ComboBox. Watch the child list filter automatically as you do.

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.

----------


## ranu

This site is really informative!!!!!!!!!
Thanx a lot!

----------

