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

Posted by Matt W on Stack Overflow See other posts from Stack Overflow or by Matt W
Published on 2010-06-18T13:15:13Z Indexed on 2010/06/18 13:43 UTC
Read the original article Hit count: 424

Filed under:
|
|
|
|

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 to have a DataSet render XML which has nodes appearing in multiple places, but I can't figure out how to do it.

Thanks,

Matt.

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET