rizel
asked on
populate treeview
I am writing a simple note taking program and somehow I got stuck with this problem.
How do I populate a treeview with this kind of data? I have little experience with treeviews...
For example:
string[] data = new string[] {"tv","tv|comedy","tv|horr or","game" ,"game|pc" ,"game|pc| genre"};
what I want my treeview to look like this:
tv
|_comedy
|_horror
game
|_pc
|_genre
I thought a recursive method would be useful...but, so far I haven't succeed.
Thank you.
How do I populate a treeview with this kind of data? I have little experience with treeviews...
For example:
string[] data = new string[] {"tv","tv|comedy","tv|horr
what I want my treeview to look like this:
tv
|_comedy
|_horror
game
|_pc
|_genre
I thought a recursive method would be useful...but, so far I haven't succeed.
Thank you.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Excellent.
It's lot different from my codes which I had struggled to make it work.
Thank you very much. :)
It's lot different from my codes which I had struggled to make it work.
Thank you very much. :)
ASKER
found a minor problem...
I have some duplicate words in node description string, the treeview won't look right due to hashtable that doesn't accept any duplicate words...
example:
test|test|test
abc|test|abc
orange|apple|apple
etc.
any solutions to this problem?
thanks
I have some duplicate words in node description string, the treeview won't look right due to hashtable that doesn't accept any duplicate words...
example:
test|test|test
abc|test|abc
orange|apple|apple
etc.
any solutions to this problem?
thanks
In that case you would need to traverse the TreeView using a Recursive algorithm....I'll post another solution for ya. =)
Try this out...
private void button1_Click(object sender, EventArgs e)
{
String[] data = new string[] {"tv|comedy", "tv|horror", "game|pc|genre", "test|test|test", "abc|test|abc", "orange|apple|apple"};
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
foreach (String path in data)
{
buildNodes(path.Split("|". ToCharArra y()), 0, null);
}
treeView1.ExpandAll();
treeView1.EndUpdate();
}
private void buildNodes(String[] path, int level, System.Windows.Forms.TreeN ode parentNode)
{
System.Windows.Forms.TreeN ode node = null;
if (parentNode == null)
{
foreach (System.Windows.Forms.Tree Node curNode in treeView1.Nodes)
{
if (curNode.Text.Equals(path[ level]))
{
node = curNode;
break;
}
}
}
else
{
foreach (System.Windows.Forms.Tree Node curNode in parentNode.Nodes)
{
if (curNode.Text.Equals(path[ level]))
{
node = curNode;
break;
}
}
}
if (node == null)
{
node = new System.Windows.Forms.TreeN ode(path[l evel]);
if (parentNode == null)
{
treeView1.Nodes.Add(node);
}
else
{
parentNode.Nodes.Add(node) ;
}
}
if (level < path.GetUpperBound(0))
{
buildNodes(path, ++level, node);
}
}
private void button1_Click(object sender, EventArgs e)
{
String[] data = new string[] {"tv|comedy", "tv|horror", "game|pc|genre", "test|test|test", "abc|test|abc", "orange|apple|apple"};
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
foreach (String path in data)
{
buildNodes(path.Split("|".
}
treeView1.ExpandAll();
treeView1.EndUpdate();
}
private void buildNodes(String[] path, int level, System.Windows.Forms.TreeN
{
System.Windows.Forms.TreeN
if (parentNode == null)
{
foreach (System.Windows.Forms.Tree
{
if (curNode.Text.Equals(path[
{
node = curNode;
break;
}
}
}
else
{
foreach (System.Windows.Forms.Tree
{
if (curNode.Text.Equals(path[
{
node = curNode;
break;
}
}
}
if (node == null)
{
node = new System.Windows.Forms.TreeN
if (parentNode == null)
{
treeView1.Nodes.Add(node);
}
else
{
parentNode.Nodes.Add(node)
}
}
if (level < path.GetUpperBound(0))
{
buildNodes(path, ++level, node);
}
}
Did this fix your problem rizel?
ASKER
Sorry, I was away for few days...I finally got a chance to write your code today.
Yes, it worked fine perfectly even with multi-language texts. :)
Thanks a bunch :)
Yes, it worked fine perfectly even with multi-language texts. :)
Thanks a bunch :)
Cool... =)
String[] data = new string[] {"tv|comedy", "tv|horror", "game|pc|genre"};