Eric, I have seen your code below, which seems to help me a great deal.
However, my table looks alittle different and I keep getting an error : Let me shopw you what my table looks like :
NodeId, NodeName, TierNumber, NodeLevel, NodeDescription, NodeDisplayOrder, NodeParentName
1, ShowCustomers,1,0,Show All Customer, Show Cusotmers,1,
2,Options,1,0,Show Options,Options,2
3,Maintenance,2,1,Show Maintenance Options,Maintenance,1,Options
4,Password,2,1,Change Password,Password,2,Options
The erros I get is on the line :
((TreeNode) ht[nodeLvl + nodeParent]).Nodes.Add(tNode);
The error is :
Additional information: Object reference not set to an instance of an object.
Here is the code I have, which is very similar to what you have written :
private void loadTree()
{
OleDbDataAdapter daSnd = null;
DataSet dsSnd = null;
String dsNameSnd = "TreeView";
bool eVal = dbCon.DbExecute (ref daSnd ,ref dsSnd, "SELECT * FROM TreeViewNodes ORDER BY NodeLevel", dsNameSnd);
if (eVal == true)
{
DataTable dt = new DataTable();
Hashtable ht = new Hashtable();
daSnd.Fill(dt);
foreach (DataRow dr in dt.Rows )
{
int nodeLvl = int.Parse(dr["NodeLevel"].ToString().Trim());
String nodeName = dr["NodeName"].ToString().Trim();
String nodeParent = dr["ParentNodeName"].ToString().Trim();
TreeNode tNode = new TreeNode(nodeName);
ht.Add(nodeLvl.ToString() + nodeName, tNode);
if (tvInt.Nodes.Count == 0)
{
tvInt.Nodes.Add(tNode);
}
else
{
nodeLvl --;
((TreeNode) ht[nodeLvl + nodeParent]).Nodes.Add(tNode);
}
}
ht.Clear();
}
}
}
Do you have an idea why I am getting that error message ?
I was also wondering if there is a way I can attach a description to each node so when the mouse hovers above it, it will be displayed.
Many thanks in advance.
Tomarsh
:
: private TreeView tView = new TreeView();
: public void PopulateTree()
: {
: DataTable dt = new DataTable();
: Hashtable ht = new Hashtable();
:
: SqlConnection conn = new SqlConnection("data source=tstsvr;initial catalog=TreeTest;integrated security=SSPI;");
:
: new SqlDataAdapter("SELECT * FROM Nodes ORDER BY NodeLevel Asc", conn).Fill(dt);
:
: foreach (DataRow r in dt.Rows)
: {
: int nodeLvl = int.Parse(r["NodeLevel"].ToString());
: string nodeParent = r["Parent"].ToString();
: string nodeName = r["Name"].ToString();
:
: TreeNode tNode = new TreeNode(nodeName);
: ht.Add(nodeLvl.ToString() + nodeName, tNode);
:
: if (tView.Nodes.Count == 0)
: tView.Nodes.Add(tNode);
: else
: {
: nodeLvl --;
: ((TreeNode) ht[nodeLvl.ToString() + nodeParent]).Nodes.Add(tNode);
: }
: }
: ht.Clear();
: }
:
:
: Hope this helps.
:
: Eric Maino
: GVSU Microsoft SA
:
: