Hi Ryan,
Thank you for posting.
I don't think you need to put the two tables Product and User_mm_Product
into one table using join selection.
Instead, you could create a dataset and add two datatables into it. You
may add a data relation between the two datatables in the dataset. Then you
could use the data relation to see whether one record in the parent table
has corresponding records in child table.
Below is the steps of my solution.
1. Create a DataSet(e.g DataSet1) and pull the two tables(Product and
User_mm_Product) from Server Explorer to the DataSet designer, which will
add two DataTables(Product and User_Product) and two
TableAdapters(ProductTableAdapter and User_mm_ProductTableAdapter).
2. Right-click the User_mm_ProductTableAdapter and select Configure
command. In the configuration wizard, add the where clause "where UserID =
@userid" to the select sql statement. Press Finish button.
3. Add a TreeView control(e.g treeView1) and two buttons(e.g button1 and
button2) on a form.
4. Fill the two datatables and populate the treeView1 with the data in the
DataTable Product in the handler for the button1 click event. Insert or
delete records in DataTable User_mm_Product according to the check status
of treenodes in treeView1.
The following is a sample code in the form.
DataSet1 dataset = new DataSet1();
// fill the two datatables and populate the treeView1
private void button1_Click(object sender, EventArgs e)
{
DataSet1TableAdapters.ProductTableAdapter productAdapter = new
DataSet1TableAdapters.ProductTableAdapter();
productAdapter.Fill(dataset.Product);
DataSet1TableAdapters.User_ProductTableAdapter
userproductAdapter = new DataSet1TableAdapters.User_ProductTableAdapter();
userproductAdapter.Fill(dataset.User_Product, 1);
DataRelation relation = new DataRelation("relation1",
dataset.Product.Columns["ProductID"],
dataset.User_Product.Columns["ProductID"]);
dataset.Relations.Add(relation);
ConstructTreeView();
}
// method to populate treeView1
private void ConstructTreeView()
{
this.treeView1.CheckBoxes = true;
TreeNode node;
for (int i = 0; i < dataset.Product.Rows.Count; i++)
{
node = new TreeNode();
node.Text = dataset.Product[i]["ProductID"].ToString() + "
" + dataset.Product[i]["ProductName"].ToString();
// assign the value of the ProductID field to the node's Tag
node.Tag = dataset.Product[i]["ProductID"];
if
(dataset.Product.Rows[i].GetChildRows("relation1").Length 0)
{
node.Checked = true;
}
else
{
node.Checked = false;
}
this.treeView1.Nodes.Add(node);
}
}
// insert or delete data in User_mm_Product table according to
check status in the treeView1
private void button2_Click(object sender, EventArgs e)
{
TreeNode node;
for (int i = 0; i < this.treeView1.Nodes.Count; i++)
{
node = this.treeView1.Nodes[i];
if (node.Checked)
{
if
(this.dataset.Product.FindByProductID(Convert.ToIn t32(node.Tag)).GetChildRow
s("relation1").Length == 0)
{
// add your code to insert a new record in
User_mm_Product
}
}
else
{
if
(this.dataset.Product.FindByProductID(Convert.ToIn t32(node.Tag)).GetChildRow
s("relation1").Length 0)
{
// add your code to delete the record in
Use_mm_Product
}
}
}
}
Hope this helps.
If you have anything unclear, please don't hesitate to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
================================================== ==========================
=============================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
With newsgroups, MSDN subscribers enjoy unlimited, free support as opposed
to the limited number of phone-based technical support incidents. Complex
issues or server-down situations are not recommended for the newsgroups.
Issues of this nature are best handled working with a Microsoft Support
Engineer using one of your phone-based incidents.
================================================== ==========================
=============================================
This posting is provided "AS IS" with no warranties, and confers no rights.