Dynamically Creating Treeview Nodes Question
-
Good Day Fellow Coders; Given this string coming out of a database field (potentially 10's of thousands of records at a time). I know that only the first sections will be unique. I don't know how many of the other sections will be there. strRef = "Section0.Section1.Section2.Section3.Section4.Section5.Section6.Section7.Section8.Section9"; etc... I want to create a treeView and I want the nodes to have these properties //what level is this node level = level; //create a tag id idx = idx + 1; //create a new tree node Node = new TreeNode(Name); //name it Node.Name = Name; //give it some text Node.Text = Name; //create the tag id Node.Tag = level + idx; //assign an image depending on level Node.ImageIndex = 1; //assign a selected image depending on level Node.SelectedImageIndex = 1; I have done this with spaghetti code about 822 lines but it's only about 97% reliable but all the properties get assigned. I have also done this using recursion it is 100% reliable but I don't know how to assign the different properties during the recursion process. I have been working on this a few weeks I have read a couple of books recommended by you folks and have learned a whole lot about how to make a folder browser but this is not quite the same. But I have not been able to solve this yet. I'm stuck can anyone get me movin in a right direction again. TIA Rafone Statistics are like bikini's... What they reveal is astonishing ... But what they hide is vital ...
-
Good Day Fellow Coders; Given this string coming out of a database field (potentially 10's of thousands of records at a time). I know that only the first sections will be unique. I don't know how many of the other sections will be there. strRef = "Section0.Section1.Section2.Section3.Section4.Section5.Section6.Section7.Section8.Section9"; etc... I want to create a treeView and I want the nodes to have these properties //what level is this node level = level; //create a tag id idx = idx + 1; //create a new tree node Node = new TreeNode(Name); //name it Node.Name = Name; //give it some text Node.Text = Name; //create the tag id Node.Tag = level + idx; //assign an image depending on level Node.ImageIndex = 1; //assign a selected image depending on level Node.SelectedImageIndex = 1; I have done this with spaghetti code about 822 lines but it's only about 97% reliable but all the properties get assigned. I have also done this using recursion it is 100% reliable but I don't know how to assign the different properties during the recursion process. I have been working on this a few weeks I have read a couple of books recommended by you folks and have learned a whole lot about how to make a folder browser but this is not quite the same. But I have not been able to solve this yet. I'm stuck can anyone get me movin in a right direction again. TIA Rafone Statistics are like bikini's... What they reveal is astonishing ... But what they hide is vital ...
-
give some sample data and an example of how you want the tree veiw to be displayed. Im unsure to your requirements
Sample data might be something like this strRef = "North Campus.Physics Building.North Side.Floor5.Room 222.East Side.Book Vault 3.1147.B0.shelf 4"; Tree should look like North Campus Physics Building North Side Floor5 Room 222 East Side Book Vault 3 1147 B0 shelf 4 Build 1259 North Side Floor1 Room 21 West Side Closet n5 shelf 4 Is this what you requested? Rafone Statistics are like bikini's... What they reveal is astonishing ... But what they hide is vital ...
-
Good Day Fellow Coders; Given this string coming out of a database field (potentially 10's of thousands of records at a time). I know that only the first sections will be unique. I don't know how many of the other sections will be there. strRef = "Section0.Section1.Section2.Section3.Section4.Section5.Section6.Section7.Section8.Section9"; etc... I want to create a treeView and I want the nodes to have these properties //what level is this node level = level; //create a tag id idx = idx + 1; //create a new tree node Node = new TreeNode(Name); //name it Node.Name = Name; //give it some text Node.Text = Name; //create the tag id Node.Tag = level + idx; //assign an image depending on level Node.ImageIndex = 1; //assign a selected image depending on level Node.SelectedImageIndex = 1; I have done this with spaghetti code about 822 lines but it's only about 97% reliable but all the properties get assigned. I have also done this using recursion it is 100% reliable but I don't know how to assign the different properties during the recursion process. I have been working on this a few weeks I have read a couple of books recommended by you folks and have learned a whole lot about how to make a folder browser but this is not quite the same. But I have not been able to solve this yet. I'm stuck can anyone get me movin in a right direction again. TIA Rafone Statistics are like bikini's... What they reveal is astonishing ... But what they hide is vital ...
ok i think i see what you want now. I will give some code guideline, sorry if its not what you need thou
foreach(DataRow row in DataTable)
{
string[] parts = row["ColumnName"].ToString().Split('.');TreeNode baseNode = RootNode;//root of tree view
foreach(string s in parts)
{TreeNode temp = CheckNodeExists(s, baseNode); if(temp == null) temp = baseNode.AddNode("ID", "Name", "Image")//what ever params you need to use baseNode = temp;
}
}public TreeNode CheckNodeExists(string nodeText, TreeNodeCollection nodes)
{
if(nodes == null || nodes.Count == 0)
return null;foreach(TreeNode node in nodes) if(node.Text == nodeText) return node; return null;
}
Please let me know if this is not what you want and i will try to offer more help
-
ok i think i see what you want now. I will give some code guideline, sorry if its not what you need thou
foreach(DataRow row in DataTable)
{
string[] parts = row["ColumnName"].ToString().Split('.');TreeNode baseNode = RootNode;//root of tree view
foreach(string s in parts)
{TreeNode temp = CheckNodeExists(s, baseNode); if(temp == null) temp = baseNode.AddNode("ID", "Name", "Image")//what ever params you need to use baseNode = temp;
}
}public TreeNode CheckNodeExists(string nodeText, TreeNodeCollection nodes)
{
if(nodes == null || nodes.Count == 0)
return null;foreach(TreeNode node in nodes) if(node.Text == nodeText) return node; return null;
}
Please let me know if this is not what you want and i will try to offer more help
maybe this would help...this is the recursion I am currently using. fullRef is the name of the variable from the database Now in here I'm not sure where I can add properties to the Nodes. string[] strArr = fullRef.Split('.'); List<string> NODES = new List<string>(); //Save nodes foreach (string str in strArr) NODES.Add(str); InsertNodes(treeView1.Nodes, NODES); } treeView1.CollapseAll(); treeView1.SelectedNode = treeView1.SelectedNode = treeView1.Nodes[0].Nodes[0].Nodes[0]; treeView1.EndUpdate(); this.Cursor = Cursors.Default; } } TreeNode AddNode(TreeNodeCollection Parent, string text) { TreeNode temporaryNode; foreach (TreeNode node in Parent) { if (node.Text == text) { return node; } } return Parent.Add(text); } void InsertNodes(TreeNodeCollection treeviewNodes, List<string> _nodes) { if (_nodes.Count == 0) return; //Recursive exit TreeNode TempNode = AddNode(treeviewNodes, _nodes[0]); _nodes.RemoveAt(0); InsertNodes(TempNode.Nodes, _nodes); //Recursive calling } TIA Rafone
Statistics are like bikini's... What they reveal is astonishing ... But what they hide is vital ...