开发者

What is the Best way to databind an ASP.NET TreeView for table with many to many parent child relationships

开发者 https://www.devze.com 2023-01-04 11:08 出处:网络
I\'ve got a table which has the usual ParentID, ChildID as it\'s first two columns in a self-referencing tree data structure.

I've got a table which has the usual ParentID, ChildID as it's first two columns in a self-referencing tree data structure.

My issue is that when I pull this out and use the following code:

DataSet set = DA.GetNewCategories();
        set.Relations.Add(
            new DataRelation("parentChildCategories", set.Tables[0].Columns["CategoryParentID"], set.Tables[0].Columns["CategoryID"])
            );
        StringBuilder buildXml = new StringBuilder();
        StringWriter writer = new StringWriter(buildXml);
        set.WriteXml(writer);
        TreeView2.DataSource = new HierarchicalDataSet(set, "CategoryID", "CategoryParentID");
        TreeView2.DataBind();

I get the error:

These columns don't currently have unique values

I believe this is because my data has children with multiple parent nodes. This is fine for my application - I don't mind if one row of data is rendered in multiple nodes of my TreeView.

Could someone shed light on this please? It doesn't seem unreasonable t开发者_如何学编程o have a DataSet render XML which has nodes appearing in multiple places, but I can't figure out how to do it.

Thanks,

Matt.


You could have a third column in your table that has ( parentID +"_"+ categoryId ) in it and than use that column in the HierarchicalDataSet. This would be unique through out your tree

And also you need to the same for the parent Ids so that they match match the category ids

0

精彩评论

暂无评论...
验证码 换一张
取 消