Treeview questions

D

DBC User

I am trying to build a tree like the following

Root
|--- Current
|-------- Date1
|-------- Date2
etc.,

First I create the root, then make the root as the selected node and
create Current and add it to the selected node. That is fine. Now I
make the Current as selected node, but, it comes back null. I am using
the following code, does anyone know why would the selected node
comeback null??

Thanks.

treeView3.Nodes.Clear();
treeView3.Nodes.Add(tRoot, tRoot); //tRoot = 'Root' string
treeView3.ExpandAll();
treeView3.SelectedNode = treeView3.Nodes[tRoot];
TreeNode cnode = new TreeNode();
cnode.Text = currentRoot; //currentRoor = 'Current' string
cnode.Name = currentRoot;
treeView3.SelectedNode.Nodes.Add(cnode);

//get treq, it is a database query to fetch all matching
rows
if (tReq.AnyRowAvailableAll())
{
tReq.Rewind();
do
{
treeView3.SelectedNode =
treeView3.Nodes[currentRoot];
if (treeView3.Nodes[tReq.AName] == null)
{
TreeNode node = new TreeNode();
node.Text = tReq.AName;
node.Name = tReq.AName;
treeView3.SelectedNode.Nodes.Add(node);
node = null;
}
} while (tReq.MoveNext());
}
 
P

Peter Thornqvist

You don't need to use the selected node. This should work just as well:

TreeNode ANode;
treeView1.Nodes.Clear();
ANode = treeView1.Nodes.Add("Root");
ANode = ANode.Nodes.Add("Current");
if (tReq.AnyRowAvailableAll())
{
tReq.Rewind();
do
{
ANode.Nodes.Add(tReq.AName, tReq.AName);
} while (tReq.MoveNext());
}
 

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