By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

Accessing DataSet values easily

P: n/a
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
Oct 11 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

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

Oct 11 '06 #2

P: n/a
Can you show me a sample of the XML file?
--
-----------
Thanks,
Steve
"sloan" wrote:
>
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


Oct 11 '06 #3

P: n/a
The reason for the example is I don't have a primary key defined as you do in
the XSD file.
--
-----------
Thanks,
Steve
"sloan" wrote:
>
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


Oct 11 '06 #4

P: n/a


Here is a sample.

dim ds as DataSet

ds = GetDataSet1()

Console.WriteLine(ds.GetXml())

............................

Private Function GetDataSet1() As DataSet

Dim ds As New DataSet

Dim sb As New System.Text.StringBuilder

sb.Append("<?xml version=""1.0""?><items>")

'sb.Append("<item>")

'sb.Append("<itemid>0</itemid>")

'sb.Append("<friendlyname1>Food</friendlyname1>")

'sb.Append("<friendlyname2>Food</friendlyname2>")

'sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

''sb.Append("<parentid></parentid>")

'sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>100</itemid>")

sb.Append("<friendlyname1>Vegetables</friendlyname1>")

sb.Append("<friendlyname2>VG</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

'sb.Append("<parentid>0</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>200</itemid>")

sb.Append("<friendlyname1>Fruits</friendlyname1>")

sb.Append("<friendlyname2>FR</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

'sb.Append("<parentid>0</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>1001</itemid>")

sb.Append("<friendlyname1>Potatoes</friendlyname1>")

sb.Append("<friendlyname2>PO</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

sb.Append("<parentid>100</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>1002</itemid>")

sb.Append("<friendlyname1>Spinach</friendlyname1>")

sb.Append("<friendlyname2>SP</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

sb.Append("<parentid>100</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>2001</itemid>")

sb.Append("<friendlyname1>Apples</friendlyname1>")

sb.Append("<friendlyname2>AP</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

sb.Append("<parentid>200</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>2002</itemid>")

sb.Append("<friendlyname1>Bananas</friendlyname1>")

sb.Append("<friendlyname2>BN</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

sb.Append("<parentid>200</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>20001</itemid>")

sb.Append("<friendlyname1>Granny Smith</friendlyname1>")

sb.Append("<friendlyname2>GS</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

sb.Append("<parentid>2001</parentid>")

sb.Append("</item>")

sb.Append("<item>")

sb.Append("<itemid>20002</itemid>")

sb.Append("<friendlyname1>Macintosh</friendlyname1>")

sb.Append("<friendlyname2>MA</friendlyname2>")

sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))

sb.Append("<parentid>2001</parentid>")

sb.Append("</item>")

sb.Append("</items>")

Dim ms As New System.IO.MemoryStream

Dim writer As New System.IO.StreamWriter(ms)

writer.Write(sb.ToString())

writer.Flush()

ms.Position = 0

ds.ReadXml(ms)

Return ds

End Function 'GetDataSet1

"SteveT" <St****@newsgroups.nospamwrote in message
news:15**********************************@microsof t.com...
The reason for the example is I don't have a primary key defined as you do
in
the XSD file.
--
-----------
Thanks,
Steve
"sloan" wrote:

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

Oct 11 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.