hello.
i get data from two database db1, db2, into one datagridview, this dtatgridview has three columns[Id, Name, DirectorId], only the first row has no director, and each Id has one or more child, so that i want to create treeview that have one main node, and each node can has one or more of childNode, for that, i declare :

......... 
TreeNode tn = new TreeNode(this.DataGridView2.Rows[0].Cells[0].Value.ToString());
tn.Tag = this.DataGridView2.Rows[0].Cells[4].Value.ToString();
treeView1.Nodes.Add(settree(tn));
}
public TreeNode settree(TreeNode ns)
{
for (int i = 1; i <= this.DataGridView2.Rows.Count-1; i++)
{
if (this.DataGridView2.Rows[i].Cells[5].Value.ToString() == ns.Tag.ToString())
{
var node = new TreeNode(this.DataGridView2.Rows[i].Cells[0].Value.ToString());
node.Tag = this.DataGridView2.Rows[i].Cells[4].Value.ToString();
node.Nodes.Add(settree(node));
return node;
}
else { return ns; }
}    }

where cell[0] for name, cell[4] for id, cell[5] for directorId.
I suppose that this code is suitable, but i have error, that i can not solve it.
when i apply this code, i get error message that tell me:(not all code paths return a value) at settree() method;
can you help me?
thank you

My guess here is because all your return statements are within a for loop. If that loop is empty, then you would never return. However, if read this correctly your for loop is not a good idea. Because if there is any items in the first loop it will ALWAYS return the first index in the loop, making a loop pretty much pointless.

It looks like you are trying to do recurssion here, however, the problem is that when you call "settree" again, you don't pass in any sort of index or point to start off at for the DataGridView2 row. So you always are going to be at index 1 everytime you call it (meaning you could theoretically get a OutOfMemory type exception because the loop really doesn't have an end)

If I get some time tonight, I'll try and look at your code to see if I can offer a possible fix to these issues

Edited 1 Year Ago by JOSheaIV

Thank you Mr. JosheaIV.
I solve this problem using this snippet code:

{
.............
TreeNode tn = new TreeNode(this.DataGridView2.Rows[0].Cells[0].Value.ToString());//text


tn.Tag = this.DataGridView2.Rows[0].Cells[4].Value.ToString();// id
tn.Name = this.DataGridView2.Rows[0].Cells[5].Value.ToString();//directorid
                treeView1.Nodes.Add(tn);
                settree(tn);
            }
            public void settree(TreeNode ns)
        {
            foreach (DataGridViewRow dr in DataGridView2.Rows)
            {
                if (dr.Cells[5].Value.ToString() == ns.Tag.ToString())
                {
                    TreeNode tsn = new TreeNode(dr.Cells[0].Value.ToString());
                    tsn.Tag = dr.Cells[4].Value.ToString();
                    tsn.Name = dr.Cells[5].Value.ToString();
                    ns.Nodes.Add(tsn);
                    settree(tsn);
                }
            }
        }



    thank you for your reply

Edited 1 Year Ago by Mohammad_21

By the way (had to run off before I could finish my post). Be careful when it comes to recursion, while the results of it could be rewarding if you implement it correctly, if you do it slightly wrong, it can be nasty! Debugging can be tricky, and if you don't for instance control indexing correctly, you can easily blow Out of Memory type errors

This article has been dead for over six months. Start a new discussion instead.