Click to See Complete Forum and Search --> : Tree View Help


buddhi
May 30th, 2006, 06:20 AM
Hi all,

Please help me to do following thing......

I have a table ..like this

point.jpg

I want to load this into a tree view as follows..

tree.jpg

Since this needs to be done using single filed on a tatble which has been delimited by "." is the issue I have.....

Please advice me..

klintan
May 30th, 2006, 08:44 AM
Order table by point name and code something like this:


string[] names={ "abc.def.1", "abc.def.2", "abc.ghi.1", "def.def.1" };
const int treeDepth=3;
TreeNodeCollection[] rootNodes=new TreeNodeCollection[treeDepth];
rootNodes[0]=treeView1.Nodes;
string[] lastRootName=new string[treeDepth];
foreach (string name in names)
{
string[] parts=name.Split(new char[] {'.'},3);
for (int i=0;i<treeDepth;++i)
{
if (parts[i]!=lastRootName[i])
{
TreeNode newNode=new TreeNode();
newNode.Text=parts[i];
rootNodes[i].Add(newNode);
if (i<treeDepth-1)
rootNodes[i+1]=newNode.Nodes;
lastRootName[i]=parts[i];
for (int j=i+1;j<treeDepth;++j)
lastRootName[j]=string.Empty;
}
}

buddhi
June 1st, 2006, 04:37 AM
Hi

Thankx a lot ....U saved my life..... :thumb:

buddhi
June 21st, 2006, 01:13 AM
Hi all ,

Again I faced small problem. I am using the above code in my programme. But once I use a string like this:

string[] names={ "ss.Branch0.Leaf0", "ss.Branch0.Leaf0.a", "ss.Branch2.Leaf0.f.g", "ss.Branch3.Leaf0" };

it thorows an error....Please help me...

klintan
June 21st, 2006, 10:44 AM
What's the error? On which line is it thrown?

Also I think that the idea of this forum is to get things started, the code I posted was to give you a hint of a possible solution, it is probably not perfect meaning that it handles any type of input. So, I think you could figure out a solution by yourself.

buddhi
June 22nd, 2006, 06:42 AM
Hi Klinton and all,

Yea, you are correct. i got your idea and done my own code. But this problem crashes my code once in a while...

I tried to figure out but I couldn't....I think you all can help me because I feel you all are experts than me. I am a new comer to .Net....

The exact error message I am getting is "Object reference not set to an instance of an object."

and it trows from here... rootNodes[i].Add(newNode);

Please help me.....

Below is my code, which I have modified to simulate the error I am getting....

try
{
string[] names={ "ss.Branch0.Leaf0", "ss.Branch0.Leaf0.a", "ss.Branch2.Leaf0.f.g", "ss.Branch3.Leaf0" };
int treeDepth=15;
TreeNodeCollection[] rootNodes=new TreeNodeCollection[treeDepth];
rootNodes[0]=treeView1.Nodes;
string[] lastRootName=new string[treeDepth];
foreach (string name in names)
{
string[] parts=name.Split(new char[] {'.'});
treeDepth = parts.Length;
for (int i=0;i<treeDepth;++i)
{
if (parts[i]!=lastRootName[i])
{
TreeNode newNode=new TreeNode();
newNode.Text=parts[i];
rootNodes[i].Add(newNode);
if (i<treeDepth-1)
rootNodes[i+1]=newNode.Nodes;
lastRootName[i] = parts[i];
for (int j = i+1;j<treeDepth;++j)
lastRootName[j] = string.Empty;
newNode = null;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

klintan
June 22nd, 2006, 07:00 AM
Well, the problem was that my code did not handle strings shorter than the max tree depth, and you tried to fix it by changing the tree depth but that didn't help either as you change the tree depth all the time.

change this code:


treeDepth = parts.Length;
for (int i=0;i<treeDepth;++i)
[/php]
to this code:


for (int i=0;i<parts.Length;++i)


And it should work.

buddhi
June 22nd, 2006, 07:58 AM
Hi Klintan,

:confused: is it not working still....What to do...it throws the same error....

And what should I do for the following place...
for (int j = i+1;j<treeDepth;++j)

I changed this to ...
for (int j = i+1;j<parts.Length;++j)

But again also same....

Please help me...

aniskhan
June 22nd, 2006, 09:16 AM
try this, changed a single line
try
{
string[] names ={ "ss.Branch0.Leaf0", "ss.Branch0.Leaf0.a", "ss.Branch2.Leaf0.f.g", "ss.Branch3.Leaf0" };
int treeDepth=15;
TreeNodeCollection[] rootNodes = new TreeNodeCollection[treeDepth];
rootNodes[0] = treeView1.Nodes;
string[] lastRootName; //---------------------------------just declare here
foreach (string name in names)
{
string[] parts = name.Split(new char[] { '.' });
treeDepth = parts.Length;
lastRootName = new string[treeDepth]; //---------------------------------now initialize here
for (int i = 0; i < treeDepth; ++i)
{
if (parts[i] != lastRootName[i])
{
TreeNode newNode = new TreeNode();
newNode.Text = parts[i];
rootNodes[i].Add(newNode);
if (i < treeDepth - 1)
rootNodes[i + 1] = newNode.Nodes;
lastRootName[i] = parts[i];
for (int j = i + 1; j < treeDepth; ++j)
lastRootName[j] = string.Empty;
newNode = null;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

buddhi
June 22nd, 2006, 11:33 PM
Hi All,

Thanking for all who contributed....

The last code posted working fine...But that is not I really wanted....Please see the attached pictures on the very first post...

All items should come under same root....

Please see below....this is the way it should presant,,,,

a
|
+ ---- b
[INDENT]|
[INDENT]+ --- c
[INDENT][INDENT]|
[INDENT][INDENT]+ ----- d

Please take this code it is more clear....It throws error while adding Node "d"....


private void button1_Click(object sender, System.EventArgs e)
{
try
{
string[] names={ "aa.bb.cc", "aa.bb.cc.dd"};
int treeDepth=15;
TreeNodeCollection[] rootNodes=new TreeNodeCollection[treeDepth];
rootNodes[0]=treeView1.Nodes;
string[] lastRootName=new string[treeDepth];
foreach (string name in names)
{
string[] parts=name.Split(new char[] {'.'});
treeDepth = parts.Length;
for (int i=0;i<parts.Length;++i)
{
if (parts[i]!=lastRootName[i])
{
TreeNode newNode=new TreeNode();
newNode.Text=parts[i];
rootNodes[i].Add(newNode);
if (i<treeDepth-1)
rootNodes[i+1]=newNode.Nodes;
lastRootName[i] = parts[i];
for (int j = i+1;j<parts.Length;++j)
lastRootName[j] = string.Empty;
newNode = null;
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}