简体   繁体   中英

TreeView in C# and MS Access with Unique ID

I have a TreeView connected to MS Access DB, Im trying to read/write the nodes to DB. I dont think my way is good because for first and second level of nodes its easy, after that its getting more hard: here is my way:

 public void LoadNodes() { ConnectionShorten("TreeViewTable"); int H = MyNodes.Tables["TreeViewTable"].Rows.Count; for (int i = 0; i < H; i++) { int PID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("ParentID"); string Name = MyNodes.Tables["TreeViewTable"].Rows[i].Field<string>("RootName"); int Level = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("Level"); int UID = MyNodes.Tables["TreeViewTable"].Rows[i].Field<int>("UID"); switch (Level) { case 0: treeView1.Nodes.Add(Name.ToString()); break; case 1: treeView1.Nodes[0].Nodes.Add(Name.ToString()); break; case 2: switch (PID) { case 1: treeView1.Nodes[0].Nodes[0].Nodes.Add(Name.ToString()); break; case 2: treeView1.Nodes[0].Nodes[1].Nodes.Add(Name.ToString()); break; case 3: treeView1.Nodes[0].Nodes[2].Nodes.Add(Name.ToString()); break; case 4: treeView1.Nodes[0].Nodes[3].Nodes.Add(Name.ToString()); break; case 5: treeView1.Nodes[0].Nodes[4].Nodes.Add(Name.ToString()); break; case 6: treeView1.Nodes[0].Nodes[5].Nodes.Add(Name.ToString()); break; case 7: treeView1.Nodes[0].Nodes[6].Nodes.Add(Name.ToString()); break; case 8: treeView1.Nodes[0].Nodes[7].Nodes.Add(Name.ToString()); break; case 9: treeView1.Nodes[0].Nodes[8].Nodes.Add(Name.ToString()); break; case 10: treeView1.Nodes[0].Nodes[9].Nodes.Add(Name.ToString()); break; case 11: treeView1.Nodes[0].Nodes[10].Nodes.Add(Name.ToString()); break; default: break; } break; case 3: break; default: break; } } }

The thing is I want enter nodes with no limit like: A->lvl1->lvl2->lvl3->...lvl100...

My English is not that good sorry for that!

EDIT1: I want to read nodes and write them to DB with just unique ID. Thats what im trying to do.

You need to implement a commonly used techique called lazy treeview loading .

This technique consists in initializing only the very first level of your tree. Then, for every node you want to be expandable, you add a subnode, call it dummy subnode. This specific subnode has a specific text you want to be distinguishable from all other texts.

 public const string DUMMYNODETEXT = "__dummyNode!!!";

 ...

 var node = new TreeNode("top level node");
 node.Nodes.Add( DUMMYNODETEXT );

 node.Tag = something_that_tags_this_node;

 treeView.Nodes.Add( node );

Then, when a tree node is expanded , you want to check if the node contains this special dummy node and if yes, only then you want to initialize the nested level.

 public void TreeView_BeforeExpand( object sender, TreeViewCancelEventArgs e )
 {
     if ( e.Node != null &&
          e.Node.Nodes.Count == 1 &&
          e.Node.Nodes[0].Text == DUMMYNODETEXT
         )
     {
         // if you get there, the node containing only the dummy node is being
         // expanded

         // first, remove the dummy node
         e.Node.Nodes.Clear();

         // then, determine which node is expanded
         var tag = e.Node.Tag;

         // depending on the tag value, create subnodes
         // but remember to add dummy nodes to your newly created subnodes
         var newNodes = CreateSubNodesForNode( tag );

         foreach ( var newNode in newNodes )
         {
             e.Node.Nodes.Add( newNode );
         }
     }
 }

 public IEnumerable<TreeNode> CreateSubNodesForNode( object tag )
 {
     if ( tag is .... your type )
     {
         return ...
     }

     if ( tag is .... another type )
     {
         return ... 
     }
 }

By following this pattern you always only initialize the node that is actually expanded. The actual database query executes only when necessary. Also, the depth of the tree doesn't introduce any difficulties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM