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?