Night Walker
Night Walker

Reputation: 21270

Representing hierarchical data .net winforms

How i can represent the following hierarchical data ? What control should be used , if possible example will help.

-node 
-----node1 - -data--data --data
-------------node111 -- data -- data
-------------node112 -- data -- data
-------------node113 -- data -- data
-----node2 - -data--data --data
-------------node1121 -- data -- data
-----node3 - -data--data --data

and if possible i need to put in most of the cells several icons .

I have found this tutorial Link , can someone support me more information ?

Is that possible if yes how ?

Thanks a lot .

Upvotes: 5

Views: 8218

Answers (3)

BillW
BillW

Reputation: 3435

One way might be to create a derived TreeNode object, force it to contain a List<data> :

// requires declaration of : using System.Windows.Forms;

// sample data class
public class data
{
    public string Name;
    public int ID;
}

public class XTreeNode : TreeNode
{
    List<data> theData = new List<data>();

    public XTreeNode(string theNodeID)
    {
        this.Text = theNodeID;
    }

    public void addData(data newData)
    {
        theData.Add(newData);
    }
}

Here's a (not elegant) sample of what building an instance of the above data structure might look like (on a WinForm) : assume you have a TreeView, named 'treeView1 on the Form :

    XTreeNode currentNode;
    data currentData;

    for (int i = 0; i < 10; i++)
    {
        // create the node and add it to the 'treeView1
        currentNode = new XTreeNode(i.ToString());
        treeView1.Nodes.Add(currentNode);

        // add some data entries to the List<data> of the derived TreeNode
        currentData = new data {Name = "one", ID = 100};
        currentNode.addData(currentData);

        currentData  = new data { Name = "two", ID = 200 };
        currentNode.addData(currentData);

        currentData = new data { Name = "three", ID = 300 };
        currentNode.addData(currentData);

        // sample of adding a child node
        currentNode.Nodes.Add(new XTreeNode((i * 100).ToString()));
    }

For the question of how you woud visually display the List<data> associated with each Node : the usual way would be to combine the Treeview with a ListView, and synchronize their locations and item heights : then display the List<data> on the same "row" as the corresponding TreeNode.

Of course you can implement your own Node and NodeCollection entities which are completely independent of any control : this example presents a mixed-case of relying on a .NET control to serve as both data structure and presentation mechanism.

There's an excellent example of a combination TreeView/ListView on CodeProject that has been maintained, updated, and extended, for years : Phillip Piper's : "A Much Easier to Use ListView", first published 2006, last update October, 2009 : its functionality is so rich that if compares favorably, imho, with commercial components.

Upvotes: 1

Aaronaught
Aaronaught

Reputation: 122644

The built-in Windows Forms controls aren't great for this. What you're actually looking for is a tree-grid hybrid control (AKA multi-column tree view or TreeList).

DevExpress has an XtraTreeList, which is what I use (not free), and is probably the closest to what you're asking for. Telerik's GridView can also display grid data in a hierarchical fashion if you set up the groupings right.

If those prices are too steep, you might try FlexibleTreeView. Or if you desperately need something free, check out this CodeProject page: Advanced TreeView for .NET. It's going to be a lot more quirky and difficult to use than the commercial products, but it will do the job.

Note that I'm assuming that the data is uniform, that you basically want to display the same data for every node in the hierarchy. If the data is heterogeneous (completely different columns depending on the type of node or level), what you actually want to use is a hierarchical GridView. You can get those from the same publishers listed above. I'm not aware of any half-decent free version.

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273244

Use a TreeView Control

Upvotes: 1

Related Questions