简体   繁体   English

与具有多对多父子关系的表数据绑定ASP.NET TreeView的最佳方法是什么

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

I've got a table which has the usual ParentID, ChildID as it's first two columns in a self-referencing tree data structure. 我有一个表,该表具有通常的ParentID,ChildID,因为它是自引用树数据结构中的前两列。

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. 这对我的应用程序来说很好-我不在乎是否在TreeView的多个节点中呈现了一行数据。

Could someone shed light on this please? 有人可以对此进行说明吗? It doesn't seem unreasonable to have a DataSet render XML which has nodes appearing in multiple places, but I can't figure out how to do it. 具有节点出现在多个位置的DataSet渲染XML似乎并不合理,但是我不知道该怎么做。

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. 您可以在表中的第三列中包含(parentID +“ _” + categoryId),然后在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 而且您还需要为父ID设置相同的名称,以便它们与类别ID匹配

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM