Let's say that one needs to do table in database for navigation bar hierarchy (or something similar like product category). And if the level of the hierarchy is undetermined (or just free) and still should be updated and stored in database, then only way to
describe is with self-referencing table. NAVIGATIONBAR ===== NB_id (PK) NB_Parent_NB_id (FK reference to NB_id) NB_Name ... ... So data could look like this: 1 NULL TV's ... 2 2 Wide-screen TV's ... 3 2 Some other tv's... ... OK. And presume that this hierarchy
should be viewed with something that is done for viewing hierarchical data. let's say TreeView web form control. OK lets load data to datatable from database: Dim ds As New DataSet("NAVBARELEMENTS") '..Connections and SQL's da.FillSchema(ds,SchemaType.Mapped,"NAVIGATIONBAR")
da.Fill(ds,"NAVIGATIONBAR") '... Now, we know that TreeView needs it's data in pre-defined XML form (one example) like this: ==== ==== Now, next task is to get XML representation of the ds. It's easy: Dim str As String=ds.GetXml() But the XML looks exacty
same kind as in datatable (self-referencing): ============== 37 TV's 38 37 Wide screen tv's 39 37 Some other tv's =============== This is different from XML syntax that TreeView demands and also this isn't hierarchical in manner that TreeView "gets" it. So,
we need DataRelation. Now lets define a datarelation and set it to nested: =============== Dim drel As New DataRelation("NAVBAR_SELF",ds.Tables("NAVIGATIONBAR").Columns("NB_id"),ds.Tables("NAVIGATIONBAR").Columns("NB_Parent_NB_id")) drel.Nested=True ds.Relations.Add(drel)
=============== Now, let's see the XML again: =============== 37 TV's 38 37 Wide screen tv's 39 Some other tv's ========= This is much,much better as it points the hierarchy in elements not just via fields (elements are nested like TreeView demands, thanks
to nested DataRelation). Only what we need to do is the XSLT Transformation of our XML to XML that TreeView understands. (this can be done separately with XslTransform class or using TreeView's TreeNodeXsltSrc Property) Anyway,needed XSLT template might look
like this: ============= ============== Result of the transformation is: ============== - - ============== Which is correct format for TreeView to view(via it's TreeNodeSrc property). I might have had typos in this example as this is written 11.55 PM (+02.00
time zone), so forgive that to me. =) But I think idea came clear. Thanks.
I think I found a bugg with the TreeView. Reason I say that is because it doesn't seem logical that Microsoft did it by design. Senario 1 TreeView With Post Back This example has the flag AutopPostBack set to True. This example works, however, when it comes
time to choose the students, I need the TreeView not to post back. As you can see in this working demo, it would take too long for a user to choose more than 3 students . . . Senario 2 TreeView Without Post Back This example has the flag AutopPostBack set
to False. In this case, the treeView doesn't work properly. It seems that the TreeView won't expand unless the AutoPostBack is True. The way I found around this is to initiate the TreeView with all the nodes expanded. But that is not scalable at all. Please
download the demo I prepared at http://fashiondepotonline.com/DOTNET/TreeView_Question.zip Let me know what you think! CelloJ
Thanks a lot this example is excellent, but for the stupid guy, what exactly do you put into that TreeNodeSrc property ??? Do you crate some function or an url or what - if there is url what should be on the page ? I have tried to assign this prop programatically
in Page_Load event TreeView1.TreeNodeSrc = myDataSet.GetXml(); - didn't work I am exhausted I am trying to make this work for like 5 hours Thx a lot migel
[Edit by="joteke"][/Edit] TreeNodeSrc takes the result of the XSLT transformation I showed in my example(as string). TreeView accepts only certain type of XML syntax and the meaning of XSLT transformation is to change dataset data to match this TreeView syntax.
It's very unlikely that you would get this data straight from dataset unless you name dataset elements according to what TreeView needs. " Result of the transformation is: ============== - - ============== Which is correct format for TreeView to view(via it's
TreeNodeSrc property). "
No, the parameter contains the XML you are going to display as a string. It's the XML got from database and it has to be restructured with XSLT before assignning it for TreeView so it's format is such that TreeView accepts it. Take a look at my example with
care and those things should come quite clear from it.
I can't make it work - here is my codes: --------------webform2.aspx.cs (codebehind)-------------- public class WebForm2 : System.Web.UI.Page { public Microsoft.Web.UI.WebControls.TreeView TreeView1; public string strXmlTxt; private void Page_Load(object sender,
System.EventArgs e) { SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]); SqlDataAdapter myCommand = new SqlDataAdapter("sp_GetAllProductCategories", myConnection); // Mark the Command as a SPROC myCommand.SelectCommand.CommandType
= CommandType.StoredProcedure; // Add nParentCategory as a parameter SqlParameter parameterParentCategory = new SqlParameter("@nIdParent", SqlDbType.Int, 4); parameterParentCategory.Value = 0; myCommand.SelectCommand.Parameters.Add(parameterParentCategory);
// Create dataset System.Data.DataSet ds = new System.Data.DataSet(); ds.DataSetName = "Categories"; myCommand.Fill(ds, "ProductCategories"); strXmlTxt = ds.GetXml(); TreeView1.TreeNodeSrc=strXmlTxt; } --------------webform2.aspx----------------------
You have it correct but you dont have TreeView1.DataBind(); after setting TreeView1.TreeNodeSrc=strXmlTxt; (TreeView is also binded when used). If it does not help then show me what you get from ds.getXML() method and how does your XSLT template look like.
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Show hierarchical data with TreeView and self-referencing Datatable
Jul 01, 2002 07:56 PM|LINK
Teemu Keiski
Finland, EU
bsapd
Member
10 Points
2 Posts
Re: Show hierarchical data with TreeView and self-referencing Datatable
Jul 11, 2002 03:29 PM|LINK
g.c.r. & associates, inc.
bestep@gcr1.com
CelloJ
Member
40 Points
8 Posts
Re: Show hierarchical data with TreeView and self-referencing Datatable
Jul 29, 2002 07:18 PM|LINK
mige
Member
35 Points
7 Posts
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 12:18 AM|LINK
http://www.mwebsnet.com
bsapd
Member
10 Points
2 Posts
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 03:16 AM|LINK
g.c.r. & associates, inc.
bestep@gcr1.com
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 04:35 AM|LINK
Teemu Keiski
Finland, EU
mige
Member
35 Points
7 Posts
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 09:53 AM|LINK
http://www.mwebsnet.com
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 11:20 AM|LINK
Teemu Keiski
Finland, EU
mige
Member
35 Points
7 Posts
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 01:23 PM|LINK
http://www.mwebsnet.com
joteke
All-Star
46224 Points
6896 Posts
ASPInsiders
MVP
Re: Show hierarchical data with TreeView and self-referencing Datatable
Aug 01, 2002 04:14 PM|LINK
Teemu Keiski
Finland, EU