473,408 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Accessing DataSet values easily

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
4 1950

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
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: George Grodentzik | last post by:
I created a typed dataset from which I am trying to access the data. When I use the following code to access a row string name =dataset.person.firstName I receive an error...
5
by: Paulos | last post by:
Hi Has anyone any experience of accessing .CSV files with ADO.NET and C#? All I want to do is open and read a number of them. I have search the help files, the MDSN and all the books I have...
3
by: Mark | last post by:
Hi - when working with datasets, is it quicker to loop through the dataset, comparing some column values with predetermined values, or should I apply a filter on the dataset to retrieve the values...
2
by: Sandy | last post by:
Hello - I have the following stored procedure and code. I want to put the results in two textboxes. I get to the part where I create the dataset and then I don't know what to do. I tried...
0
by: N. Demos | last post by:
Hello, I'm having problems accessing a complex XML child node (latitude & longitude), and passing it to a function when the XML file has been read into a DataSet. Specifically, the returned object...
2
by: Peter Van Wilrijk | last post by:
Hi, In VB6 I used a method requery to repopulate a recordset. I don't find an equivalent for the .NET dataset. I don't update my datasets at all, but I use them to navigate through subsets...
2
by: Bob | last post by:
I have a CreatedOn field , datetime, which has GetDate() as the default value in SQL server 2000 table. When I create a new record in the table itself in enterprise manager, the field gets...
2
by: paolol | last post by:
Hi, I wont to get the current row values from a dataset, any one know how to reach those data ?? At the moment I use a work around, but it's not good .. foreach (DataRow row in...
2
by: Paul | last post by:
Hi all, I've written a custom DAL for a web app i'm currently working on, and I am trying to make it as efficient as possible- where possible. Objects are populated on creation using the DAL,...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.