I'm working on a simple windows application using SQL Server Compact with Entity Framework 6, I have a TreeView which is populated by a database, there is also a DataGrid which should be filled based on the selected node from TreeView. It looks like this [Image](http://i.imgur.com/7i94onK.jpg/ "Image")

Database Model:

* **Categories**
	+ CategoryID
	+ CategoryName
* **SubCategories**
	+ SubID
	+ SubName
	+ CategoryID
* **Items**
	+ ItemID
	+ ItemName
	+ SubID

---

I used this code to fill the DataGrid, everything was working just fine, but it won't save new rows to the database, editing existed rows is working though.

```
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
    if (treeView.SelectedNode.Level == 1)
    {
        BindingSource bs = new BindingSource();
        bs.DataSource = (from a in context.Items where a.SubID == (int)treeView.SelectedNode.Tag select a).ToList();
        dataGrid.DataSource = bs;
    }
}  

```

---

So, I tried this. Saving new rows and editing existed ones worked, the thing is when selecting a node in TreeView it should only show the **Items** linked to the selected node, but it doesn't it adds the new query result to the old one, displaying both queries on the DataGrid, would be probably more if I selected more nodes.

```
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
{
    if (treeView.SelectedNode.Level == 1)
    {
        context.Items.Where(a => a.SubID == (int)treeView.SelectedNode.Tag).Load();
        dataGrid.DataSource = context.Items.Local.ToBindingList();
    }
} 

```

---

There is one more thing I'm trying to do, when selecting a **Sub-Category** from TreeView I should get **Items** linked to this particular **Sub-Category** displayed on DataGrid, so is it possible when adding a new row in DataGrid it automatically set the **SubID** field in **Items** to the ID of selected node, linking this newly added **item** to the selected **Sub-Category** in TreeView.

Any help would be appreciated, and sorry if the solution is obvious, but all of this is new to me. Thanks.