set the parent property of a treenode in treeview control

G

Guest

How can I set the parent property of a treenode in vb.net after i have
populated the treeview with nodes to give the hierachical view?
for instance :

onode.parent= Parentnode

I cannot do this now because it is a read only property.
 
L

Linda Liu [MSFT]

Hi,
Thank you for posting. From your post, my understanding on this issue is:
how to set the parent property of a treenode to give a hierachical view.
If I'm off base, please feel free to let me know.

All the properties related to the hierachical view of a treenode are
readonly . To give a hierachical view, you should use the
treeNode1.Nodes.Add() method.

The following is a sample.
dim node1 as TreeNode = New TreeNode("node 1")
dim node2 as TreeNode = New TreeNode("node 2")
// set node1 as node2's parent
node1.Nodes.Add(node2)

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Thank you for your responce. Here is my problem.
I open a table in an access database with the following structure:

NodeID NodeText ParentID
1 Root -1
2 Node1 1
3 Node2 1
4 Node3 2
5 Node 4 1
6 Node 5 4
7 Node 6 6

Now I wish to just go through this table using a datareader object
preferably and add them to the treeview.
It was easy in vb6 using the Parent Property to assign the parent but
can you give an example on acheiving this with vb.net?
Thank you
Giannis
 
J

Jeff Gaines

Thank you for your responce. Here is my problem.
I open a table in an access database with the following structure:

NodeID NodeText ParentID
1 Root -1
2 Node1 1
3 Node2 1
4 Node3 2
5 Node 4 1
6 Node 5 4
7 Node 6 6

Now I wish to just go through this table using a datareader object
preferably and add them to the treeview.
It was easy in vb6 using the Parent Property to assign the parent but
can you give an example on acheiving this with vb.net?
Thank you
Giannis

Giannis

I have done this in C# if you can convert it:

You call it from your app like this:

private void TVLoadNodesFromDB(ref TreeView tv)
{
cNodes cn = new cNodes();
cn.FillTV(ref tv);
}

The work is done in a class called cNodes like this:

public bool FillTV(ref TreeView tv)
{
ADODB.Connection adCON = new ADODB.Connection();
ADODB.Recordset adRS = new ADODB.Recordset();

bool blnOK = false;
int intID, intParent, intIndex, intTag;
string strNodeText, strTag;

TreeNode tnNew;

string strConnection;
string strQuery;

// Open Connection
strConnection = "Provider=Microsoft.Jet.OLEDB.4.0;";
strConnection += " Data Source=";
strConnection += m_strDBName;
strConnection += ";";
adCON.Open(strConnection, "", "", 0);

// Loop
intID = 0;

strQuery = "SELECT * FROM " + this.m_strTableName + " WHERE intID =
";
strQuery += intID.ToString();

//try
{

adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset,
ADODB.LockTypeEnum.adLockOptimistic, 0);

if (!adRS.EOF)
{
intID = Convert.ToInt32(cFunc.GetField(adRS, "intID"));
intParent = Convert.ToInt32(cFunc.GetField(adRS, "intParent"));
strNodeText = cFunc.GetField(adRS, "strNodeText");
intIndex = Convert.ToInt32(cFunc.GetField(adRS, "intIndex"));
strTag = cFunc.GetField(adRS, "strTag");

tnNew = new TreeNode(strNodeText);
tnNew.Tag = strTag;
tv.Nodes.Add(tnNew);

intTag = Convert.ToInt32(strTag);
if (intTag > this.m_HighestTag)
this.m_HighestTag = intTag;

adRS.Close();

// Now the children
GetChildrenRecursive(intID, ref tnNew, adCON);
}

adCON.Close();
blnOK = true;
}
//catch
//{
// blnOK = false;
//}

cFunc.TVHighestTag = this.m_HighestTag;

return blnOK;
}

private void GetChildrenRecursive(int intParent, ref TreeNode
tnParent, ADODB.Connection adCON)
{
ADODB.Recordset adRS = new ADODB.Recordset();

int intID, intIndex, intTag;
string strNodeText, strTag;

string strQuery = "SELECT * FROM " + this.m_strTableName + " WHERE
intParent = ";
strQuery += intParent.ToString();
strQuery += " ORDER BY intIndex";

TreeNode tnChild;

adRS.Open(strQuery, adCON, ADODB.CursorTypeEnum.adOpenKeyset,
ADODB.LockTypeEnum.adLockOptimistic, 0);

while (!adRS.EOF)
{
intID = Convert.ToInt32(cFunc.GetField(adRS, "intID"));
intParent = Convert.ToInt32(cFunc.GetField(adRS, "intParent"));
strNodeText = cFunc.GetField(adRS, "strNodeText");
intIndex = Convert.ToInt32(cFunc.GetField(adRS, "intIndex"));
strTag = cFunc.GetField(adRS, "strTag");

tnChild = new TreeNode(strNodeText);
tnChild.Tag = strTag;
tnParent.Nodes.Add(tnChild);

intTag = Convert.ToInt32(strTag);
if (intTag > this.m_HighestTag)
this.m_HighestTag = intTag;

// Now the children
GetChildrenRecursive(intID, ref tnChild, adCON);

adRS.MoveNext();
}
adRS.Close();
}

Does that help?
 
L

Linda Liu [MSFT]

Hi Giannis,
Thank you for your reply.
If you want to go through the table using a DataReader object and add the
record to a treeview, you may go though the table for several times just
like Jeff's answer.
I think you can retrieve all the data from the Database table to a
DataTable first and then generate TreeNode for each record. Thus you need
access the database only once. And the sequent process happens in the
memory.
The following is a sample.

DataTable table = new DataTable();
private void button1_Click(object sender, System.EventArgs e)
{
// retrieve data from the database table to DataTable "table"
.......
// add all the data to a treeview
BuildTree();
}
private void BuildTree()
{
TreeNode treeNode = null;
ArrayList firstLevelNodes = GetChild();
for(int i = 0; i < firstLevelNodes.Count; i++)
{
treeNode = BuildSubTree((DataRow)firstLevelNodes);
this.treeView1.Nodes.Add(treeNode);
}
}
// use a recursion arithmetic to build the subTree
private TreeNode BuildSubTree(DataRow row)
{
TreeNode treeNode = new TreeNode(Convert.ToString(row["NodeText"]));
ArrayList childArray = GetChild(Convert.ToInt32(row["NodeID"]));
TreeNode childNode = null;
for(int i = 0; i < childArray.Count; i++)
{
childNode = BuildSubTree((DataRow)childArray);
treeNode.Nodes.Add(childNode);
}
return treeNode;
}
private ArrayList GetChild()
{
ArrayList array = new ArrayList();
foreach(DataRow row in table.Rows)
{
if (Convert.ToInt32(row["ParentID"]) == -1)
{
array.Add(row);
}
}
return array;
}
private ArrayList GetChild(int parentID)
{
ArrayList array = new ArrayList();
foreach(DataRow row in table.Rows)
{
if (Convert.ToInt32(row["ParentID"]) == parentID)
{
array.Add(row);
}
}
return array;
}

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.

Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

thanks linda.
Yes it is best to touch the database only once for the records.
Is it possible in some way to obtain the code in vb.net ?
Regards
Giannis
 
L

Linda Liu [MSFT]

Hi Giannis,

Thank you for your reply. There is the sample written in Vb.net.

Dim table As DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

' retrieve data from the database table to DataTable "table"
...................
' add all the data to a treeview
BuildTree()

End Sub

Private Sub BuildTree()

Dim treeNode As TreeNode
Dim firstLevelNodes As ArrayList = GetChild()
For i As Integer = 0 To firstLevelNodes.Count - 1
treeNode = BuildSubTree(firstLevelNodes(i))
Me.TreeView1.Nodes.Add(treeNode)
Next i

End Sub

Private Function BuildSubTree(ByRef row As DataRow) As TreeNode

Dim treeNode As TreeNode = New
TreeNode(Convert.ToString(row.Item("NodeText")))
Dim childArray As ArrayList =
GetChild(Convert.ToInt32(row.Item("NodeID")))
Dim childNode As TreeNode
For i As Integer = 0 To childArray.Count - 1
childNode = BuildSubTree(childArray(i))
treeNode.Nodes.Add(childNode)
Next i
Return TreeNode

End Function

Private Function GetChild() As ArrayList

Dim array As ArrayList = New ArrayList()
For Each row As DataRow In table.Rows
If (Convert.ToInt32(row.Item("ParentID")) = -1) Then
array.Add(row)
End If
Next row
Return array

End Function

Private Function GetChild(ByVal parentID As Integer) As ArrayList

Dim array As ArrayList = New ArrayList()
For Each row As DataRow In table.Rows
If Convert.ToInt32(row.Item("ParentID")) = parentID Then
array.Add(row)
End If
next row
Return array

End Function

Hope this is helpful to you.
If you have any other concerns or need anything else, please don't hesitate
to tell me.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top