asp.net Treeview issue (can't get child nodes to display) - c#

I have been working on this for a while, but cannot resolve the issue. I have searched S/O & Google, but no luck. Hoping someone on here can help resolve this.
I am not able to display the child nodes in my TreeView control. The data is being retrieved from a database.
The root node appears fine, but there are not child nodes displayed. How can I get the child nodes to be displayed?
My code is:
private void PopulateTreeNode(DataSet dsList)
{
var treeNode = new TreeNode();
foreach (DataRow dr in dsList.Tables[0].Rows)
{
if (dr["RecordTypeID"].ToString() == "1")
{
TreeNode NewNode = new TreeNode(dr["CustomerName"].ToString(), dr["customerID"].ToString());
treeCustomer.Nodes.Add(NewNode);
}
else if (dr["RecordTypeID"].ToString() == "2")
{
TreeNode pNode = new TreeNode(dr["CustomerName"].ToString(), dr["customerID"].ToString());
pNode.ChildNodes.Add(pNode);
}
else if (dr["RecordTypeID"].ToString() == "3")
{
TreeNode pNode = new TreeNode(dr["CustomerName"].ToString(), dr["customerID"].ToString());
pNode.ChildNodes.Add(pNode);
}
}
treeCustomer.Nodes.Add(treeNode);
treeCustomer.DataBind();
}

You don't need to call treeCustomer.DataBind() if you are manually adding nodes like this. It is probably clearing out your tree.

Have you tried calling treeCustomer.ExpandAll() ?
You may also need to set various properties of the treeview too; regarding displaying expand images etc..

It looks like you're adding pNode to the child nodes of pNode. This means it is never getting added to treeCustomer since only treeNode is being added to treeCustomer. It should probably be added to the child nodes of treeNode, or possibly added as a node of treeNode.

Related

why in treeView1 when i make CollapseAll and Expand it's not really doing it ?

When i'm running the program i see the root node
Countries
Then when i click on it i see all the countries nodes under Countries
But i want when running the program that it already will show all the countries nodes without clicking on Countries.
I tried in the constructor:
PopulateTree(mainPath, treeView1.Nodes.Add("Countries"));
treeView1.CollapseAll();
treeView1.Nodes[0].Expand();
The populatetree
public void PopulateTree(string dir, TreeNode node)
{
DirectoryInfo directory = new DirectoryInfo(dir);
foreach (DirectoryInfo d in directory.GetDirectories())
{
TreeNode t = new TreeNode(d.Name);
PopulateTree(d.FullName, t);
node.Nodes.Add(t);
}
foreach (FileInfo f in directory.GetFiles())
{
TreeNode t = new TreeNode(f.Name);
node.Nodes.Add(t);
}
}
But it's not doing it i still see Countries when running the program and to see all the childs nodes i need to click on Countries.
This lines not effect
treeView1.CollapseAll();
treeView1.Nodes[0].Expand();
TreeNode.Expand expands only Nodes[0] down to the next level of nodes. You should use TreeNode.ExpandAll to expand all child nodes of Countries node:
treeView1.Nodes[0].ExpandAll()
NOTE: There is one thing you should keep in mind. If handle is not created for TreeView control, then something like lazy collapsing-expanding is working here. I.e. each node has expandOnRealization and collapseOnRealization fields. When you are trying to expand node before tree handle is created, then just expandOnRealization flag is set to true. No TVM_EXPAND windows messages are sent to actually expand that node. Same for collapsing. When tree node is realized, then there is following code executed:
// If node expansion was requested before the handle was created,
// we can expand it now.
if (expandOnRealization) {
Expand();
}
// If node collapse was requested before the handle was created,
// we can expand it now.
if (collapseOnRealization) {
Collapse();
}
So, if node was marked both for collapsing and expanding, then it would be expanded first and then collapsed. I believe it's your case.

Clearing TreeView item ChildNodes also clears Parent reference?

I'm developing an ASP.NET 4.0 web application and I am using a TreeView control. I have one problem that I really can't understand why this is happening. I have checkboxes associated with the tree items.
If the checkbox is not checked everything works just fine, but when I execute the following line:
node.ChildNodes.Clear();
If the checkbox is checked for the node, the node.Parent is null after the Clear(). If it is not checked, parent is still correct. I really can't understand why the clearing of the childnodes collection will also clear my parent reference. I actually don't use or modify the checked property within this call. I am also 100% sure that my parent isn't a child to my node as well (why would it differ if it checked or not).
The workaround I need to use in order for the code to work as expected is:
TreeNode[] checkedNodes = new TreeNode[tvResults.CheckedNodes.Count];
tvResults.CheckedNodes.CopyTo(checkedNodes, 0);
foreach (TreeNode checkedNode in checkedNodes) // Uncheck all nodes temporary.
checkedNode.Checked = false;
node.ChildNodes.Clear(); // Now it is ok to clear and parent is "saved".
foreach (TreeNode checkedNode in checkedNodes) // Recheck all checked nodes again.
checkedNode.Checked = true;
Any idea anyone about this issue?
Thanks,
Mattias
PS! Please be kind, this is my first question ever...
Below is the code from ASP.NET TreeNodeCollection.Clear (from .NET 4.5). As you can see, it does clear all checked nodes without performing any validation. So this is by design (rather a bug by design) and not a bug in your code. My advice would be add an extension method that performs the workaround you listed in the question so that you can easily reuse it.
// System.Web.UI.WebControls.TreeNodeCollection
/// <summary>Empties the <see cref="T:System.Web.UI.WebControls.TreeNodeCollection" /> object.</summary>
public void Clear()
{
if (this.Count == 0)
{
return;
}
if (this._owner != null)
{
TreeView owner = this._owner.Owner;
if (owner != null)
{
if (owner.CheckedNodes.Count != 0)
{
owner.CheckedNodes.Clear();
}
for (TreeNode treeNode = owner.SelectedNode; treeNode != null; treeNode = treeNode.Parent)
{
if (this.Contains(treeNode))
{
owner.SetSelectedNode(null);
break;
}
}
}
}
foreach (TreeNode current in this._list)
{
current.SetParent(null);
}
this._list.Clear();
this._version++;
if (this._isTrackingViewState)
{
this.Log.Clear();
}
this.Log.Add(new TreeNodeCollection.LogItem(TreeNodeCollection.LogItemType.Clear, 0, this._isTrackingViewState));
}

Copy all Selected (Checked) TreeNodes from one treeview to another (include unchecked parents) C#

I've created a directory and file browser TreeView on the left side. I want the users to be able to browse the tree and check the files and directories that they would like to move to another treeview.
The other TreeView is a user control I found online called TreeViewColumn. I will be using that control to allow the user to add other data (categories, attributes) to the files and folders selected.
The trouble I'm running into is two-fold, I need to recursively add all children (I can figure this out) but I need to add unchecked parents to checked children (to preserve the hierarchy).
private void IterateTreeNodes(TreeNode originalNode, TreeNode rootNode)
{
//Take the node passed through and loop through all children
foreach (TreeNode childNode in originalNode.Nodes)
{
// Create a new instance of the node, will need to add it to the recursion as a root item
// AND if checked it needs to get added to the new TreeView.
TreeNode newNode = new TreeNode(childNode.Text);
newNode.Tag = childNode.Tag;
newNode.Name = childNode.Name;
newNode.Checked = childNode.Checked;
if (childNode.Checked)
{
// Now we know this is checked, but what if the parent of this item was NOT checked.
//We need to head back up the tree to find the first parent that exists in the tree and add the hierarchy.
if (tvSelectedItems.TreeView.Nodes.ContainsKey(rootNode.Name)) // Means the parent exist?
{
tvSelectedItems.TreeView.SelectedNode = rootNode;
tvSelectedItems.TreeView.SelectedNode.Nodes.Add(newNode);
}
else
{
AddParents(childNode);
// Find the parent(s) and add them to the tree with their CheckState matching the original node's state
// When all parents have been added, add the current item.
}
}
IterateTreeNodes(childNode, newNode);
}
}
private TreeNode AddParents(TreeNode node)
{
if (node.Parent != null)
{
//tvDirectory.Nodes.Find(node.Name, false);
}
return null;
}
Could anyone help with this code so that it recursively adds checked nodes (and their parent, regardless of checked state). I need to maintain directory hierarchy.
Thanks for any help!
A rather (not-so-clean and not-preferred) solution could be to clone the tree first, and then remove unchecked branches.
Else, when you are adding a node, write a recursive method to traverse through node's parent node until you hit the root. And simply optimize it by checking if childNode.parent already exists, just ignore the branch and move on. i.e. Backtrack to root node.
I got it working. I was already aware of what #Yahya was saying, I was hoping for an easier way / better approach.
The code below is certainly not optimal, I will continue improving it on my end but at this point it is looking through a treeview on the left and copying all of the checked items (and their parents - regardless of CheckedState) to a treeview on the right.
Hopefully this helps someone and thanks for answering #Yahya.
I'm open to improvements but keep in mind this is a one-time use utility.
private void cmdMoveRight_Click(object sender, EventArgs e)
{
tvSelectedItems.TreeView.Nodes.Clear();
// Traverse first level Tree Nodes
foreach (TreeNode originalNode in tvDirectory.Nodes)
{
TreeNode newNode = new TreeNode(originalNode.Text);
newNode.Name = originalNode.Name;
newNode.Tag = originalNode.Tag;
newNode.Checked = originalNode.Checked;
//Only add to the new treeview if the node is checked
if (originalNode.Checked)
{
tvSelectedItems.TreeView.Nodes.Find(originalNode.Parent.Name,true)[0].Nodes.Add(newNode);
}
//Start recursion - this will be called for each first level node - there should technically only be 1 "ROOT" node.
IterateTreeNodes(originalNode, newNode);
}
}
private void IterateTreeNodes(TreeNode originalNode, TreeNode rootNode)
{
//Take the node passed through and loop through all children
foreach (TreeNode childNode in originalNode.Nodes)
{
// Create a new instance of the node, will need to add it to the recursion as a root item
// AND if checked it needs to get added to the new TreeView.
TreeNode newNode = new TreeNode(childNode.Text);
newNode.Tag = childNode.Tag;
newNode.Name = childNode.Name;
newNode.Checked = childNode.Checked;
if (childNode.Checked)
{
// Now we know this is checked, but what if the parent of this item was NOT checked.
//We need to head back up the tree to find the first parent that exists in the tree and add the hierarchy.
TreeNode[] nodestest = tvSelectedItems.TreeView.Nodes.Find(childNode.Parent.Name, true);
if (nodestest.Length > 0)
{
tvSelectedItems.TreeView.Nodes.Find(childNode.Parent.Name,true)[0].Nodes.Add(newNode);
}
else
{
AddParents(childNode);// Find the parent(s) and add them to the tree with their CheckState matching the original node's state
}
}
//recurse
IterateTreeNodes(childNode, newNode);
}
}
private void AddParents(TreeNode node)
{
if (node.Parent != null)// Check if parent is null (would mean we're looking at the root item
{
TreeNode[] nodestest = tvSelectedItems.TreeView.Nodes.Find(node.Parent.Name, true);
if (nodestest.Length > 0)
{
TreeNode[] nodes = tvDirectory.Nodes.Find(node.Name, true);
TreeNode newNode = new TreeNode(nodes[0].Text);
newNode.Name = nodes[0].Name;
newNode.Tag = nodes[0].Tag;
newNode.Checked = nodes[0].Checked;
tvSelectedItems.TreeView.Nodes[node.Parent.Name].Nodes.Add(newNode);
}
else
{
AddParents(node.Parent);
TreeNode newNode = new TreeNode(node.Text);
newNode.Name = node.Name;
newNode.Tag = node.Tag;
newNode.Checked = node.Checked;
tvSelectedItems.TreeView.Nodes.Find(node.Parent.Name,true)[0].Nodes.Add(newNode);
}
}
else // deal with root node
{
TreeNode rootNode = new TreeNode(node.Text);
rootNode.Name = node.Name;
rootNode.Tag = node.Tag;
rootNode.Checked = node.Checked;
tvSelectedItems.TreeView.Nodes.Add(rootNode);
}
}

Tree View not showing update

I have a TreeView in my Windows Form user interface.
I want to fill it up from a database, but it does not refresh ever, even though if I WriteLine() every node, it is in memory as I expect.
In order to make it more easy to understand, I wrote a little example program that only has one button that creates a TreeView and a TreeView called treeView1 to display its content.
If anyone can tell me where I have misunderstood the use of the TreeView, it would be a tremendous help.
private void button1_Click(object sender, EventArgs e)
{
// create a tree
TreeView t = new TreeView();
TreeNode[] child = new TreeNode [1];
child[0]=new TreeNode("myCat");
child[0].Name = "IndependantOne";
TreeNode categoryNode = new TreeNode("catIdTag", child);
categoryNode.Name = "Citizen Cat 5239002147";
t.Nodes.Add(categoryNode);
// some stuff under the first node
TreeNode[] mouseNode = new TreeNode[1];
mouseNode[0] = new TreeNode("myMouse");
mouseNode[0].Name = "SqueakyOne";
TreeNode[] childItem = new TreeNode[1];
childItem[0] = new TreeNode("mouseIdTag", mouseNode);
childItem[0].Name = "Citizen Mouse 54655654649";
TreeNode eltNode = new TreeNode("Cheese", childItem);
eltNode.Name = "Emmental";
t.Nodes["Citizen Cat 5239002147"].Nodes.Add(eltNode);
// fill in the winform treeview
if (t != null)
{
//treeView1.Visible = false;
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
treeView1.TopNode = t.TopNode;
foreach (TreeNode n in t.Nodes)
{
treeView1.Nodes.Add(n);
System.Diagnostics.Debug.WriteLine("Category Node contains: " + treeView1.Nodes[n.Name].Name + " at " + treeView1.Nodes[n.Name].Level);
foreach (TreeNode no in treeView1.Nodes[n.Name].Nodes)
{
System.Diagnostics.Debug.WriteLine("Category Node Nodes contains: " + no.Name);
}
}
/*
This part I tried and it doesn't work either, still add it in the question if anyone knows if it's wiser?
this.treeView1 = t;
this.treeView1.Location = new System.Drawing.Point(233, 12);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(351, 277);
this.treeView1.TabIndex = 11;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
*/
treeView1.EndUpdate();
this.treeView1.Update();
this.treeView1.Refresh();
treeView1.Show();
//this.Refresh();
}
}
I also tried setting the treeView1 with
treeView1 = t;
It was not a success...
change
treeView1.Nodes.Add(n);
with
treeView1.Nodes.Add((TreeNode)n.Clone());
You cannot host the same node in more than one TreeView control.
Alternatively, you can remove nodes from source TreeView before adding them to other TreeView:
while(t.Nodes.Count != 0)
{
var node = t.Nodes[0];
t.Nodes.RemoveAt(0);
treeView1.Nodes.Add(node);
}
And I hope that there is a real reason why you create and fill tree view in your method instead of directly filling an already existing one. If it is not an intended behavior, remove the if block completely and change TreeView t = new TreeView(); to var t = treeView1.
This behavior is expected because with this code you`re confusing node-to-treeview relationship nature of the native .NET treeview control. Instead when moving nodes between treeviews (t -> treeView1) you need to clone them as suggested. Without that moved node still linked with the old treeview (see node.Treeview property) and because original tree (t) is not visible/added to any parent (form), I guess node will be invisible as well.
Also, the way you`re working with data loading (through creating new treeview) is pretty bad pattern. Instead you need to download your data (async I guess) into a temp buffer and recreate the treeView1 at once when data will be available with BeginUpdate/EndUpdate calls.
PS. Replacing treeView1 variable with 't' won't work as well because you don't replace the treeview control instance in the parent form/panel Controls property with this code.

child node removing from treeview in c#

I need to remove particular child nodes from their parent in treeview control.
For example:
Before Login
home
-register
-login
-pdf
After login
home
-pdf
What is the best way to accomplish this?
should be like...
//This will remove login
TreeNode tn = TreeView1.FindNode("home/login"); // find particular node
TreeView1.Nodes[0].ChildNodes.Remove(tn); // then remove from TreeView
//This will remove register
tn = TreeView1.FindNode("home/register"); // find particular node
TreeView1.Nodes[0].ChildNodes.Remove(tn); // then remove from TreeView
Adding child node in parent node programmatically:
TreeNode tn = new TreeNode("login");
tn.NavigateUrl = "/home/login";
TreeView1.Nodes[0].ChildNodes.Add(tn);

Categories