Create a new strong typed DataSet. Call it AnyRecursiveSituation
Paste in the following code (in the xml view if in 1.1)
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="AnyRecursiveSituation"
targetNamespace="http://tempuri.org/AnyRecursiveSituation.xsd"
elementFormDefault="qualified" attributeFormDefault="qualified"
xmlns="http://tempuri.org/AnyRecursiveSituation.xsd"
xmlns:mstns="http://tempuri.org/AnyRecursiveSituation.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="AnyRecursiveSituation" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="anyentity">
<xs:complexType>
<xs:sequence>
<xs:element name="itemid" type="xs:int" minOccurs="0" />
<xs:element name="friendlyname1" type="xs:string" minOccurs="0" />
<xs:element name="friendlyname2" type="xs:string" minOccurs="0"
nillable="true" />
<xs:element name="parentid" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:key name="AnyRecursiveSituationKey1" msdata:PrimaryKey="true">
<xs:selector xpath=".//mstns:anyentity" />
<xs:field xpath="mstns:itemid" />
</xs:key>
<xs:keyref name="myRecursiveRelationshipName"
refer="AnyRecursiveSituationKey1">
<xs:selector xpath=".//mstns:anyentity" />
<xs:field xpath="mstns:parentid" />
</xs:keyref>
</xs:element>
</xs:schema>
The key is to create a relationship.
And then you keep getting the children of the relationship.
Sorry, I only have the vb.net code on this one:
Public Class RecursiveBrowserTreeNodeGrower
Public Shared Function GrowTreeView(ByRef srcTreeView As TreeView, ByVal
friendlyValueText() As String, ByVal friendlyValueDelim As String, ByVal
idValues() As String, ByVal idDelimiter As String, ByVal rowResults As
DataRow(), ByVal parentNode As TreeNode, ByVal childRelationName As String)
As TreeView
Dim parentrow As DataRow
Dim nodeCurrentLevelToAdd As TreeNode
Dim rootNodeExistsCheck As Boolean = True
Dim i As Int16 = 0 'loop for the array args
Try
'this Try/Catch makes sure a root node exists, if not, there is
logic to create one
Dim rootNodeCheck As New TreeNode
rootNodeCheck = (srcTreeView.Nodes(0))
rootNodeExistsCheck = True
Catch ex As Exception
rootNodeExistsCheck = False
End Try
If rootNodeExistsCheck = False Then
If parentNode Is Nothing Then
'in case the parentNode isn't defined (at the root level)
'this will create a default rootNode
parentNode = New TreeNode '("--Expand--") 'set your own
default values here for the root node
parentNode.Text = "--Expand--"
parentNode.ID = "0" 'set your own default values here for
the root node
End If
srcTreeView.Nodes.Add(parentNode)
'note about this IF loop, it should only ever be executed
'on the "first" call, the recursive calls should never get here
End If
For Each parentrow In rowResults
'this is the friendly value
'notice you can use a "compound" friendly value because of the
array string as a parameter
Dim friendlyValue As String = ""
For i = 0 To friendlyValueText.Length - 1
friendlyValue += parentrow(friendlyValueText(i))
If i <friendlyValueText.Length - 1 And
(friendlyValueText.Length 0) Then
friendlyValue += friendlyValueDelim
End If
Next
nodeCurrentLevelToAdd = New TreeNode '(friendlyValue)
nodeCurrentLevelToAdd.Text = friendlyValue
'this is the "ID" value (the "not seen" value)
'this can also be a "compound" value, but most times it won't
'be, it will be the unique identifier on the row
Dim idValue As String = ""
For i = 0 To idValues.Length - 1
idValue += Convert.ToString(parentrow(idValues(i)))
If i <idValues.Length - 1 And (idValues.Length 0) Then
idValue += idDelimiter
End If
Next
nodeCurrentLevelToAdd.ID = idValue
'the node to add has been defined, now add this node to its
parent node
parentNode.Nodes.Add(nodeCurrentLevelToAdd)
''''populate child'''''
Dim childnode As TreeNode
childnode = New TreeNode
Dim childRows As DataRow()
childRows = parentrow.GetChildRows(childRelationName)
'here is the recursive call
GrowTreeView(srcTreeView, friendlyValueText, friendlyValueDelim,
idValues, idDelimiter, childRows, nodeCurrentLevelToAdd, childRelationName)
Next parentrow
'don't forget to return the orignal object!'don't forget to return
the orignal object!
Return srcTreeView
End Function
End Class
"SteveT" <St****@newsgroups.nospamwrote in message
news:62**********************************@microsof t.com...
I am wanting to populate several treeviews, one for the <TRs>
group and one for the <TGsgroup. Is there a simplier way
to populate the Treeview than the one I did below? It seems
difficult to get to the correct DataSet values.
<?xml version="1.0" encoding="utf-8" ?>
<TestSample>
<TRs>
<TR1>
<TSs Parent="trParent1">
<TS>ts1</TS>
<TS>ts2</TS>
<TS>ts3</TS>
</TSs>
</TR1>
<TR1>
<TSs Parent="trParent2">
<TS>ts4</TS>
<TS>ts5</TS>
</TSs>
</TR1>
</TRs>
<TGs>
<TR1>
<TSs Parent="tgParent1">
<TS>tg1</TS>
<TS>tg2</TS>
</TSs>
</TR1>
<TR1>
<TSs Parent="tgParent2">
<TS>tg3</TS>
<TS>tg4</TS>
<TS>tg5</TS>
</TSs>
</TR1>
<TR1>
<TSs Parent="tgParent3">
<TS>tg6</TS>
<TS>tg7</TS>
</TSs>
</TR1>
</TGs>
</TestSample>
protected override void OnLoad(EventArgs e)
{
// Do the base loading
base.OnLoad(e);
// Do my custom stuff
this.readXml.Read();
this.myDataSet = this.readXml.MyDataSet;
this.LoadFromDataSet();
}
private void LoadFromDataSet()
{
string str = "TRs";
DataTable dt = this.myDataSet.Tables[str];
if (dt != null)
{
foreach (DataRow r1 in dt.Rows)
{
foreach (DataRow r2 in r1.GetChildRows(str+"_TR1"))
{
foreach (DataRow r3 in r2.GetChildRows("TR1_TSs"))
{
TreeNode parent = new
TreeNode(r3["Parent"].ToString());
foreach (DataRow r4 in r3.GetChildRows("TSs_TS"))
{
TreeNode child = new
TreeNode(r4["TS_Text"].ToString());
parent.Nodes.Add(child);
}
this.treeView1.Nodes.Add(parent);
}
}
}
}
}
--
-----------
Thanks,
Steve