1

I have one List type of my own struct.This is my struct.

 public struct outlineData
    {
        public string paragraphID;
        public string outlineText;
        public int outlineLevel;
    }

and my List is

List<outlineData> outlinePara = new List<outlineData>();

So, i have added so many outlineData in my outlinePara List.Now, i want to create a TreeView based on the outlineData's outlineLevel.

for example : outlineData's outlineLevel may be 0,1,2,3,1,2....0,1...0,1,1,1,1,1,2,3,2....

So, Now i want to create a Treeview like that...

          0
             1
               2
                 3
             1
               2
          0
             1
          0
             1
             1
             1
             1
             1
               2
                 3
               2




TreeNode childNode;
            if (outlineParaInfo.outlineLevel == 0)
            {
                headNode = new TreeNode(outlineParaInfo.outlineText);
                TreeView11.Nodes.Add(headNode);
            }
            else if (outlineParaInfo.outlineLevel == 1)
            {
                childNode = new TreeNode(outlineParaInfo.outlineText);
                headNode.ChildNodes.Add(childNode);
            }

Please guide me to get a correct logic for this problem...

4
  • Please include the code you already have, and the issue you are experiencing with it. Is the tree not indented correctly? Are all the nodes at the same level? Are you missing any nodes? What's "wrong"? Commented May 11, 2012 at 11:59
  • @JoshuaDrake : I have added my code...After that i have a confusion to create TreeNode for another level... Commented May 11, 2012 at 12:11
  • Thank you for adding the code and explaining, I've edited my shot at it below. Commented May 11, 2012 at 12:25
  • It would be easier to work with your data if you had parentParagraphID, with this Level thing you can't know for sure which outlineData is parent for another Commented May 11, 2012 at 12:36

3 Answers 3

1

[EDITED TO USE System.Web.UI.WebControls.TreeView PER OP COMMENT]

How about something like the following pseudo code:

int currentLevel = 0; 
TreeNode currentNode = null;
TreeNode childNode = null;

foreach(var outLineItem in outlinePara)
{
    if(outLineItem.outlineLevel == 0)
    {
            currentNode = new TreeNode(
                                outLineItem.paragraphID, outLineItem.outlineText);

            yourTreeView.Nodes.Add(currentNode);
            continue;
    }


    if(outLineItem.outlineLevel > currentLevel)
    {
        childNode = new TreeNode(
                          outLineItem.paragraphID, outLineItem.outlineText);

        currentNode.ChildNodes.Add(childNode);
        currentNode = childNode;

        currentLevel = outLineItem.outlineLevel;
        continue;
    }

    if(outLineItem.outlineLevel < currentLevel)
    {
       currentNode = currentNode.Parent;

       childNode = new TreeNode(
                          outLineItem.paragraphID, outLineItem.outlineText);

       currentNode.ChildNodes.Add(childNode);

       currentLevel = outLineItem.outlineLevel;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

:Thanks for your effort.But i want to create actual treeview like that i mentioned... i just given an example view for your understanding...
@Saravanan you'll need to be more specific then, do you want the JavaScript to collapse and expand it? Do you want the pretty little icons for +/-?
I have added TreeView control in Asp.net(C#).Now i want to create the TreeNode for that TreeeView as per the outlineLevel...
:Thank you very much.I created a Tree view by modifying your code.It working good.I attached it as my answer...
0

Like Antonio Bakula said, use parentParagraphID. You can populate a treeview dynamically by using a recursive function

Change your outlineLevel with parentParagraphID.

public struct outlineData
{
    public string paragraphID;
    public string outlineText;
    //public int outlineLevel;
    public string parentParagraphID;
}

After creating your list.

List<outlineData> outlinePara = new List<outlineData>();

First insert the root TreeNode, then insert the rest.

TreeNode rNode = new rNode();
rNode.Name = "root";
outlinePara.Add(rNode);
...
outlinePara.Add(lastNode);

After inserting all the nodes, just launch this function.

populate(rNode, rNode.Name);

This function will create your level structure for your TreeView.

public void populate(TreeNode node, string parentParID)
{
    var newList = this.outlinePara.Where(x => x.parentParagraphID == parentParID).toList();
    foreach(var x in newList)
    {
        TreeNode child = new TreeNode();
        child.Name = paragraphID;
        child.Text = outlineText;
        populate(child, child.Name);
        node.Nodes.Add(child);
    }
}

You will get a TreeView like this

root
|-->0
|   |-->1
|   |-->1
|       |-->2
|       |-->2
|       |-->2
|
|-->0
|   |-->1
|   ...
...

Tip

In this code the ID is a string, it's much better to use something else, and it has to be unique, so you won't get a problem with your data placed in another parentNode.

Comments

0
 private void generateTreeView()
        {
            int currentLevel = 0;
            TreeNode currentNode = null;
            TreeNode childNode = null;


            foreach (var outLineItem in outlineParagraph)
            {
                if (outLineItem.outlineLevel == 0)
                {
                    currentNode = new TreeNode(outLineItem.outlineText);
                    TreeView11.Nodes.Add(currentNode);

                    currentLevel = outLineItem.outlineLevel;
                    continue;
                }

                if (outLineItem.outlineLevel > currentLevel)
                {
                    childNode = new TreeNode(outLineItem.outlineText);

                    currentNode.ChildNodes.Add(childNode);
                    currentNode = childNode;

                    currentLevel = outLineItem.outlineLevel;
                    continue;
                }

                if (outLineItem.outlineLevel < currentLevel)
                {
                    //logic to find exact outlineLevel parent...
                    currentNode = findOutlineLevelParent(outLineItem.outlineLevel, currentNode);

                    if(currentNode!=null)
                        currentNode = currentNode.Parent;

                    childNode = new TreeNode(outLineItem.outlineText);
                    currentNode.ChildNodes.Add(childNode);

                    currentLevel = outLineItem.outlineLevel;
                    currentNode = childNode;
                    continue;
                }

                if (outLineItem.outlineLevel == currentLevel)
                {
                    currentNode = currentNode.Parent;
                    childNode = new TreeNode(outLineItem.outlineText);
                    currentNode.ChildNodes.Add(childNode);

                    currentLevel = outLineItem.outlineLevel;
                    currentNode = childNode;
                    continue;
                }
            }

        }//generateTreeView Ends here...

        private TreeNode findOutlineLevelParent(int targetLevel, TreeNode currentNode)
        {

            while (currentNode.Parent != null)
            {
                currentNode = currentNode.Parent;
                if (currentNode.Depth == targetLevel)
                {
                    return currentNode;
                }
            }

            return null;
        }   //findOutlineLevelParent ends here...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.