Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. treeview?this problem has Troubled me for few days

treeview?this problem has Troubled me for few days

Scheduled Pinned Locked Moved C#
helpbusinessquestion
6 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    miss YY
    wrote on last edited by
    #1

    table A Field: a(char(40)) b(char(40)) q -- ss q -- fs q -- ffr q -- egg w -- ffh w -- gykk e -- hrh e -- kyk e -- jjtj the Purpose: field "a" as father node,field "b" as child node,field "a" should not be repeated!this is part of my code,but it can't meet my Requirements. public void AddTree(TreeNode pNode) { DataSet ds=new DataSet(); DataView dvTree = new DataView(ds.Tables[0]); foreach (DataRowView Row in dvTree) { if (pNode == null) { //add father node TreeNode Node = treeView1.Nodes.Add(Row["a"].ToString()); } else { TreeNode Node = pNode.Nodes.Add(Row["b"].ToString()); } } it only can show feild "a" on treeview,like this: q q q q w w e e e it repeat the Field 'a',can someone help me ,thanks very much!! :laugh: ;P always waiting online

    P 1 Reply Last reply
    0
    • M miss YY

      table A Field: a(char(40)) b(char(40)) q -- ss q -- fs q -- ffr q -- egg w -- ffh w -- gykk e -- hrh e -- kyk e -- jjtj the Purpose: field "a" as father node,field "b" as child node,field "a" should not be repeated!this is part of my code,but it can't meet my Requirements. public void AddTree(TreeNode pNode) { DataSet ds=new DataSet(); DataView dvTree = new DataView(ds.Tables[0]); foreach (DataRowView Row in dvTree) { if (pNode == null) { //add father node TreeNode Node = treeView1.Nodes.Add(Row["a"].ToString()); } else { TreeNode Node = pNode.Nodes.Add(Row["b"].ToString()); } } it only can show feild "a" on treeview,like this: q q q q w w e e e it repeat the Field 'a',can someone help me ,thanks very much!! :laugh: ;P always waiting online

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Looking at the sample you've posted, pNode will always be null, hence the reason a is shown again and again. What you need to do is:

      if (pNode == null)
      { //add father node
      pNode = treeView1.Nodes.Add(Row["a"].ToString());
      }
      else
      {
      TreeNode Node = pNode.Nodes.Add(Row["b"].ToString());
      }

      "WPF has many lovers. It's a veritable porn star!" - Josh Smith

      As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

      My blog | My articles | MoXAML PowerToys | Onyx

      M 2 Replies Last reply
      0
      • P Pete OHanlon

        Looking at the sample you've posted, pNode will always be null, hence the reason a is shown again and again. What you need to do is:

        if (pNode == null)
        { //add father node
        pNode = treeView1.Nodes.Add(Row["a"].ToString());
        }
        else
        {
        TreeNode Node = pNode.Nodes.Add(Row["b"].ToString());
        }

        "WPF has many lovers. It's a veritable porn star!" - Josh Smith

        As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

        My blog | My articles | MoXAML PowerToys | Onyx

        M Offline
        M Offline
        miss YY
        wrote on last edited by
        #3

        i want get the Results like q(father node) ..ff(child node) ..grt ..trt ..hth w ..trt ..fgh ..hth e ..htj ..htr ..trt ..ht which is your changed code shown: q(father node) ..ff(child node) ..grt ..trt ..hth ..trt ..fgh ..hth ..htj ..htr ..trt ..ht if do thisif (pNode == null){ //add father node pNode = treeView1.Nodes.Add(Row["a"].ToString());}else{ TreeNode Node = pNode.Nodes.Add(Row["b"].ToString());} pNode=null; shown only father node like: q w e

        P 1 Reply Last reply
        0
        • P Pete OHanlon

          Looking at the sample you've posted, pNode will always be null, hence the reason a is shown again and again. What you need to do is:

          if (pNode == null)
          { //add father node
          pNode = treeView1.Nodes.Add(Row["a"].ToString());
          }
          else
          {
          TreeNode Node = pNode.Nodes.Add(Row["b"].ToString());
          }

          "WPF has many lovers. It's a veritable porn star!" - Josh Smith

          As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

          My blog | My articles | MoXAML PowerToys | Onyx

          M Offline
          M Offline
          miss YY
          wrote on last edited by
          #4

          i want get the Results like q(father node) ..ff(child node) ..grt ..trt ..hth w ..trt ..fgh ..hth e ..htj ..htr ..trt ..ht which is your changed code shown: q(father node) ..ff(child node) ..grt ..trt ..hth ..trt ..fgh ..hth ..htj ..htr ..trt ..ht if do this if (pNode == null) { //add father node pNode = treeView1.Nodes.Add(Row["a"].ToString()); } else { TreeNode Node = pNode.Nodes.Add(Row["b"].ToString()); } pNode=null; shown only father node like: q w e

          1 Reply Last reply
          0
          • M miss YY

            i want get the Results like q(father node) ..ff(child node) ..grt ..trt ..hth w ..trt ..fgh ..hth e ..htj ..htr ..trt ..ht which is your changed code shown: q(father node) ..ff(child node) ..grt ..trt ..hth ..trt ..fgh ..hth ..htj ..htr ..trt ..ht if do thisif (pNode == null){ //add father node pNode = treeView1.Nodes.Add(Row["a"].ToString());}else{ TreeNode Node = pNode.Nodes.Add(Row["b"].ToString());} pNode=null; shown only father node like: q w e

            P Offline
            P Offline
            Pete OHanlon
            wrote on last edited by
            #5

            To be honest, your logic has more fundamental problems. You have two columns here which you are trying to work with, but the logic you have in the if/else only applies to one column level. What you could do is reimplement your logic like this:

            foreach (DataRow dr in ds.Tables[0])
            {
            string colName = dr["a"].ToString();
            TreeNode node = treeView1.Find(colName);
            if (node == null)
            {
            node = treeView1.Nodes.Add(colName, colName);
            }
            node.Nodes.Add(dr["b"].ToString());
            }

            What you are doing here is adding a node with a specific key into the treeview - and this key is the value of column a. You look for this node using the Find method and add it in if it doesn't exist. Finally, you add column b to the node.

            "WPF has many lovers. It's a veritable porn star!" - Josh Smith

            As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

            My blog | My articles | MoXAML PowerToys | Onyx

            M 1 Reply Last reply
            0
            • P Pete OHanlon

              To be honest, your logic has more fundamental problems. You have two columns here which you are trying to work with, but the logic you have in the if/else only applies to one column level. What you could do is reimplement your logic like this:

              foreach (DataRow dr in ds.Tables[0])
              {
              string colName = dr["a"].ToString();
              TreeNode node = treeView1.Find(colName);
              if (node == null)
              {
              node = treeView1.Nodes.Add(colName, colName);
              }
              node.Nodes.Add(dr["b"].ToString());
              }

              What you are doing here is adding a node with a specific key into the treeview - and this key is the value of column a. You look for this node using the Find method and add it in if it doesn't exist. Finally, you add column b to the node.

              "WPF has many lovers. It's a veritable porn star!" - Josh Smith

              As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.

              My blog | My articles | MoXAML PowerToys | Onyx

              M Offline
              M Offline
              miss YY
              wrote on last edited by
              #6

              thanks for you,but when runing the code you have give me it shows: "System.Windows.Forms.TreeView" does not contain a "Find" Definition?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups