loading a treeeview from Database

G

Guest

Hi,

I am trying to load my items from Database to a Treeview but i have faild

My table contains id, parentId, nodeName like following:

(1 , 1 RootNode)

(2 , 2 , ParentNode)

(3, 2 , Child_for_2)

(4, 4 , AnotherParent)

(5 , 2 , anotherChild_for_2)

(6 , 4 , Child_for_4)



My problem is ,how i can create a treeview from this rows ( need some code).

here is a bit of my code what i have been trying for 6 hours.

try
{
treeView.BeginUpdate();


DataSet DS = new DataSet();
DS = MyWebService.LoadTreeViewFromDB();
if (DS != null)
{
TreeNode Root = new TreeNode("Taxonomy");
Root.Tag = 1;
treeView.Nodes.Add(Root);
TreeNode ParentNode = null;

foreach (DataRow DR in DS.Tables["folksonomy"].Rows)
{
TreeNode NewNode = new TreeNode();

if (DR["id"].ToString() == DR["parentid"].ToString())
{
NewNode.Text = DR["nodename"].ToString();
NewNode.Tag = DR["id"].ToString();
Root.Nodes.Add(NewNode);
}

else
{
foreach (DataRow ChildDataRow in DS.Tables["folksonomy"].Rows)
{
if (ChildDataRow["id"].ToString() == DR["parentid"].ToString())
{
I dont know what i should write here
}
}
}

}
}
 
M

Michael Nemtsev

Hello Medes,

See sample there http://www.codeproject.com/cs/miscc...g=1169975&mode=all&userid=1430102#xx1169975xx

BTW, why not to use XML for this, like over there http://support.microsoft.com/default.aspx?scid=kb;en-us;308063


M> Hi,
M>
M> I am trying to load my items from Database to a Treeview but i have
M> faild
M>
M> My table contains id, parentId, nodeName like following:
M>
M> (1 , 1 RootNode)
M>
M> (2 , 2 , ParentNode)
M>
M> (3, 2 , Child_for_2)
M>
M> (4, 4 , AnotherParent)
M>
M> (5 , 2 , anotherChild_for_2)
M>
M> (6 , 4 , Child_for_4)
M>
M> My problem is ,how i can create a treeview from this rows ( need some
M> code).
M>
M> here is a bit of my code what i have been trying for 6 hours.
M>
M> try
M> {
M> treeView.BeginUpdate();
M> DataSet DS = new DataSet();
M> DS = MyWebService.LoadTreeViewFromDB();
M> if (DS != null)
M> {
M> TreeNode Root = new TreeNode("Taxonomy");
M> Root.Tag = 1;
M> treeView.Nodes.Add(Root);
M> TreeNode ParentNode = null;
M> foreach (DataRow DR in DS.Tables["folksonomy"].Rows)
M> {
M> TreeNode NewNode = new TreeNode();
M> if (DR["id"].ToString() == DR["parentid"].ToString())
M> {
M> NewNode.Text = DR["nodename"].ToString();
M> NewNode.Tag = DR["id"].ToString();
M> Root.Nodes.Add(NewNode);
M> }
M> else
M> {
M> foreach (DataRow ChildDataRow in DS.Tables["folksonomy"].Rows)
M> {
M> if (ChildDataRow["id"].ToString() == DR["parentid"].ToString())
M> {
M> I dont know what i should write here
M> }
M> }
M> }
M> }
M> }
---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top