Messy TreeView Results

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • pbd22

    Messy TreeView Results

    Hi.

    I am getting odd treeview results and hope you can help.

    I am parsing a string, "x/y/z", turning it into an array (that
    always seems to start with an empty string) and then
    using the elements of that array to populate a single tree.
    The first non-empty element is the root, the last is a file -
    everything in between is a folder.

    THE PROBLEM is that I am getting the root with an
    empty folder as one tree, then the next element with
    all the other nodes below it.

    So,

    array [1], [2], [3], [4]

    Should produce the following results:

    -- [1] (root folder)
    -------[2] (folder)
    ------------[3] (folder)
    ------------[4] (file)

    But, I am getting

    --[1] (root folder)
    ------[?] (empty folder with no name)

    --[2] (folder)
    ------[3] (folder)
    ------[4] (file)

    And this pattern repeats down the grid.

    Below is the code, I would really appreciate some
    clear input help here. Thanks.

    public void OnDataBinding(o bject sender, EventArgs e)
    {

    int count = 0;

    TreeView tree = (TreeView)sende r;
    GridViewRow container = (GridViewRow)tr ee.NamingContai ner;

    if (storedNodes.Co unt != 0)
    {

    string path = storedNodes.Pop ().ToString();
    string[] arr = path.Split('/');

    root = new TreeNode();
    root.ImageUrl = "folder.gif ";

    if (arr[0] != "")
    root.Text += arr[0].ToString();
    else if (arr[1] != "")
    root.Text += arr[1].ToString();

    root.SelectActi on = TreeNodeSelectA ction.SelectExp and;
    root.PopulateOn Demand = true;
    root.SelectActi on = TreeNodeSelectA ction.Expand;

    for (int i = 1; i <= arr.Length - 1; i++)
    {

    if (arr[i] != "")
    {

    TreeNode node = new TreeNode();

    if (i != arr.Length - 1)
    {

    node.ImageUrl = "folder.gif ";
    node.Text += arr[i].ToString();
    node.SelectActi on =
    TreeNodeSelectA ction.SelectExp and;
    node.PopulateOn Demand = true;
    node.SelectActi on =
    TreeNodeSelectA ction.Expand;

    }
    else
    {

    node.ImageUrl = "play.jpg";
    node.Text += arr[i].ToString();
    node.SelectActi on = TreeNodeSelectA ction.None;
    node.Value = "vBrickTree ";
    node.SelectActi on =
    TreeNodeSelectA ction.Expand;

    }

    root.ChildNodes .Add(node);
    count++;

    }

    }

    tree.Nodes.Add( root);


    }

    }
  • Mark Rae [MVP]

    #2
    Re: Messy TreeView Results

    "pbd22" <dushkin@gmail. comwrote in message
    news:c36f6bdd-70a1-41a6-903e-2d51376d1c07@f1 3g2000hsa.googl egroups.com...
    for (int i = 1; i <= arr.Length - 1; i++)
    What happens if you use:

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


    --
    Mark Rae
    ASP.NET MVP


    Comment

    • pbd22

      #3
      Re: Messy TreeView Results


      Hi.

      Thanks for responding.

      I get an "InxedOutOfBoun dsException" :

      for (int i = 1; i <= arr.Length; i++)
      {

      if (arr[i] != "") < --- EXCEPTION THROWN HERE, WHERE
      i = 4
      {


      The Current Range is:

      [0] ""
      [1] "Utah"
      [2] "Jackson"
      [3] "Climbing.w mv"

      Comment

      • Mark Rae [MVP]

        #4
        Re: Messy TreeView Results

        "pbd22" <dushkin@gmail. comwrote in message
        news:224329dd-f6a0-44f2-b7c2-2a07926bbed1@e4 g2000hsg.google groups.com...
        Thanks for responding.
        >
        I get an "InxedOutOfBoun dsException" :
        >
        for (int i = 1; i <= arr.Length; i++)
        With the greatest of respect, what happens if you actually try the code I
        suggested...?


        --
        Mark Rae
        ASP.NET MVP


        Comment

        • pbd22

          #5
          Re: Messy TreeView Results

          On Nov 15, 1:40 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
          "pbd22" <dush...@gmail. comwrote in message
          >
          news:224329dd-f6a0-44f2-b7c2-2a07926bbed1@e4 g2000hsg.google groups.com...
          >
          Thanks for responding.
          >
          I get an "InxedOutOfBoun dsException" :
          >
          for (int i = 1; i <= arr.Length; i++)
          >
          With the greatest of respect, what happens if you actually try the code I
          suggested...?
          >
          --
          Mark Rae
          ASP.NET MVPhttp://www.markrae.net
          Sorry, I did try the code you suggested.
          You are talking about the 1, right? I had
          it at zero but played around and then, sloppily,
          cut and paste the "played around" version
          here. But, the result is as i said - out of bounds
          (when i = 0 or 1)..

          Thanks again.

          Comment

          • Mark Rae [MVP]

            #6
            Re: Messy TreeView Results

            "pbd22" <dushkin@gmail. comwrote in message
            news:b592ac3a-1d75-407d-841a-aa0792063b79@v4 g2000hsf.google groups.com...
            You are talking about the 1, right?
            Not specifically...

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


            --
            Mark Rae
            ASP.NET MVP


            Comment

            • pbd22

              #7
              Re: Messy TreeView Results

              On Nov 15, 2:52 pm, "Mark Rae [MVP]" <m...@markNOSPA Mrae.netwrote:
              "pbd22" <dush...@gmail. comwrote in message
              >
              news:b592ac3a-1d75-407d-841a-aa0792063b79@v4 g2000hsf.google groups.com...
              >
              You are talking about the 1, right?
              >
              Not specifically...
              >
              for (int i = 0; i < arr.Length; i++)
              >
              --
              Mark Rae
              ASP.NET MVPhttp://www.markrae.net
              i am sorry. i am doing this in between
              meetings and am making more of a
              mess than I started with.

              I did i=0 as you suggested. for an
              array from [0] to [3], where arr.Length = 4
              and i=0 I get an array out of bounds
              exception.

              Thanks for being patient.

              Comment

              • Mark Rae [MVP]

                #8
                Re: Messy TreeView Results

                "pbd22" <dushkin@gmail. comwrote in message
                news:e87d7a5d-8336-4cd5-b3e5-d96fe6bbee10@b3 2g2000hsa.googl egroups.com...
                I did i=0 as you suggested. for an
                array from [0] to [3], where arr.Length = 4
                and i=0 I get an array out of bounds
                exception.
                >
                Thanks for being patient.
                There must be an error somewhere else, then...

                Arrays are zero-based by default...

                So, if your array has four elements, the first will be at index [0] etc...

                Have you tried stepping through the code...?


                --
                Mark Rae
                ASP.NET MVP


                Comment

                • pbd22

                  #9
                  Re: Messy TreeView Results

                  This seems to work.
                  Does it look right to you?

                  I am getting the nodes I want but the
                  tree is not displaying as a stair case, more of
                  a pyramid standing on its bottom edge.

                  string[] arr = path.Split('/');

                  for (int i = 0; i <= arr.Length -1; i++)
                  {

                  if (arr[i] != "")
                  {

                  child = new TreeNode();
                  child.ImageUrl = "folder.gif ";
                  child.Text += arr[i].ToString();
                  child.PopulateO nDemand = true;
                  child.ChildNode s.Add(child);
                  tree.Nodes.Add( child);

                  }
                  }

                  Comment

                  • Mark Rae [MVP]

                    #10
                    Re: Messy TreeView Results

                    "pbd22" <dushkin@gmail. comwrote in message
                    news:d8cca590-99d3-45a3-bc64-b03374f1bf8e@f3 g2000hsg.google groups.com...
                    This seems to work.
                    Does it look right to you?
                    Pretty much.
                    for (int i = 0; i <= arr.Length -1; i++)
                    I guess it comes down to preference, but I was always taught that

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

                    gave better performance because it didn't need to check whether the
                    incrementing variable was EITHER less than OR equal to the upper boundary of
                    the array, and didn't have to perform the additional calculation of
                    subtracting 1 from the upper boundary of the array...

                    Probably makes no difference to IL, though...


                    --
                    Mark Rae
                    ASP.NET MVP


                    Comment

                    • pbd22

                      #11
                      Re: Messy TreeView Results

                      it's odd.
                      In Firefox, the folders are in a perfect vertical line.
                      In IE, sort of a pyramid on its side.

                      I think maybe the former child is not a subset
                      of the previous child?

                      Comment

                      • pbd22

                        #12
                        Re: Messy TreeView Results

                        in other words, why am I need seeing the staircase effect of treeview?

                        Comment

                        Working...