简体   繁体   中英

Display hierarchy tree using WinForms DevExpress XtraGrid c#

I have hierarchical data structure that consists of objects like:

Class A
{
*some properties
*list<A> SubNodes
}

Right now I need to display this hierarchy in some kind of visual tree. I'm using WinForms C#/ Can you please advice me how I can disply tree like that. Standart TreeView have no DataSource property and DevExpress XtraTreeView use DataSource plain source with Id/ParentID relation. I've used XtraGrid, but I cant configure details view correctly. Could you please tell me in which way I could handle this problem?

UPD

During day duscussions was born decision that TreeList doesn't suit us. Cause we need all that stuff with autofilter row and grouppanel (where I can drag column for grouping) that provides XtraGrid. So if there is no solution to implement such tree using XtraGrid I should close this question.

You can use TreeList.IVirtualTreeListData interface.
Here is example:

public class A
{
    public A(string property0, string property1, string property2)
    {
        Property0 = property0;
        Property1 = property1;
        Property2 = property2;

        SubNodes = new List<A>();
    }

    public string Property0 { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public List<A> SubNodes { get; private set; }
}

public class TreeDataSource : List<A>, TreeList.IVirtualTreeListData
{
    void TreeList.IVirtualTreeListData.VirtualTreeGetChildNodes(VirtualTreeGetChildNodesInfo info)
    {
        var a = info.Node as A;

        info.Children = a.SubNodes;
    }

    void TreeList.IVirtualTreeListData.VirtualTreeGetCellValue(VirtualTreeGetCellValueInfo info)
    {
        var a = info.Node as A;

        switch (info.Column.FieldName)
        {
            case "Property0":
                info.CellData = a.Property0;
                break;
            case "Property1":
                info.CellData = a.Property1;
                break;
            case "Property2":
                info.CellData = a.Property2;
                break;
        }
    }

    void TreeList.IVirtualTreeListData.VirtualTreeSetCellValue(VirtualTreeSetCellValueInfo info)
    {
        var a = info.Node as A;

        switch (info.Column.FieldName)
        {
            case "Property0":
                a.Property0 = (string)info.NewCellData;
                break;
            case "Property1":
                a.Property1 = (string)info.NewCellData;
                break;
            case "Property2":
                a.Property2 = (string)info.NewCellData;
                break;
        }
    }
}

Usage of example:

var treeDataSource = new TreeDataSource();
//Add top level nodes
treeDataSource.Add(new A("Node 0, Property 0", "Node 0, Property 1", "Node 0, Property 2"));
treeDataSource.Add(new A("Node 1, Property 0", "Node 1, Property 1", "Node 1, Property 2"));
treeDataSource.Add(new A("Node 2, Property 0", "Node 2, Property 1", "Node 2, Property 2"));
//Add subnodes for first node.
treeDataSource[0].SubNodes.Add(new A("Node 0.0, Property 0", "Node 0.0, Property 1", "Node 0.0, Property 2"));
treeDataSource[0].SubNodes.Add(new A("Node 0.1, Property 0", "Node 0.1, Property 1", "Node 0.1, Property 2"));
treeDataSource[0].SubNodes.Add(new A("Node 0.2, Property 0", "Node 0.2, Property 1", "Node 0.2, Property 2"));
//Add subnodes for second node.
treeDataSource[1].SubNodes.Add(new A("Node 1.0, Property 0", "Node 1.0, Property 1", "Node 1.0, Property 2"));
treeDataSource[1].SubNodes.Add(new A("Node 1.1, Property 0", "Node 1.1, Property 1", "Node 1.1, Property 2"));
treeDataSource[1].SubNodes.Add(new A("Node 1.2, Property 0", "Node 1.2, Property 1", "Node 1.2, Property 2"));

var treeList = new TreeList();

treeList.Columns.AddField("Property0").Visible = true;
treeList.Columns.AddField("Property1").Visible = true;
treeList.Columns.AddField("Property2").Visible = true;

treeList.DataSource = treeDataSource;

Controls.Add(treeList);
treeList.Dock = DockStyle.Fill;            

Result of example:
示例结果

There are two recommended approaches for binding business object that encapsulates a tree structure to DevExpress TreeList:

1) To adapt the object for display in the TreeList control. In this case the object must implement a DevExpress.XtraTreeList.IVirtualTreeListData interface. See this article , for more information.
Example: How to: Implement a Tree Structure for a Business Object

2) To handle specific TreeList's events and to provide data for root and child nodes. See this article , for more information.
Example: How to: Load Data Dynamically via Events

If you want to use XtraGrid for hierarchy, than you can use IRelationList interface.
Here is example:

public class A
{
    public A(string property0, string property1, string property2)
    {
        Property0 = property0;
        Property1 = property1;
        Property2 = property2;

        SubNodes = new List<A>();
    }

    public string Property0 { get; set; }
    public string Property1 { get; set; }
    public string Property2 { get; set; }

    public List<A> SubNodes { get; private set; }
}

public class DataSource : List<A>, IRelationList
{
    IList IRelationList.GetDetailList(int index, int relationIndex)
    {
        return this[index].SubNodes;
    }

    string IRelationList.GetRelationName(int index, int relationIndex)
    {
        return null;
    }

    bool IRelationList.IsMasterRowEmpty(int index, int relationIndex)
    {
        return this[index].SubNodes.Count == 0;
    }

    int IRelationList.RelationCount
    {
        get { return 1; }
    }
}

Usage of example:

var dataSource = new DataSource();
//Add top level nodes
dataSource.Add(new A("Node 0, Property 0", "Node 0, Property 1", "Node 0, Property 2"));
dataSource.Add(new A("Node 1, Property 0", "Node 1, Property 1", "Node 1, Property 2"));
dataSource.Add(new A("Node 2, Property 0", "Node 2, Property 1", "Node 2, Property 2"));
//Add subnodes for first node.
dataSource[0].SubNodes.Add(new A("Node 0.0, Property 0", "Node 0.0, Property 1", "Node 0.0, Property 2"));
dataSource[0].SubNodes.Add(new A("Node 0.1, Property 0", "Node 0.1, Property 1", "Node 0.1, Property 2"));
dataSource[0].SubNodes.Add(new A("Node 0.2, Property 0", "Node 0.2, Property 1", "Node 0.2, Property 2"));
//Add subnodes for second node.
dataSource[1].SubNodes.Add(new A("Node 1.0, Property 0", "Node 1.0, Property 1", "Node 1.0, Property 2"));
dataSource[1].SubNodes.Add(new A("Node 1.1, Property 0", "Node 1.1, Property 1", "Node 1.1, Property 2"));
dataSource[1].SubNodes.Add(new A("Node 1.2, Property 0", "Node 1.2, Property 1", "Node 1.2, Property 2"));
//Add subnodes for second node in fisrt subnode.
dataSource[0].SubNodes[1].SubNodes.Add(new A("Node 0.1.0, Property 0", "Node 0.1.0, Property 1", "Node 0.1.0, Property 2"));
dataSource[0].SubNodes[1].SubNodes.Add(new A("Node 0.1.1, Property 0", "Node 0.1.1, Property 1", "Node 0.1.1, Property 2"));
dataSource[0].SubNodes[1].SubNodes.Add(new A("Node 0.1.2, Property 0", "Node 0.1.2, Property 1", "Node 0.1.2, Property 2"));

var gridControl = new GridControl();

var view = new GridView(gridControl);           

gridControl.MainView = view;
gridControl.DataSource = dataSource;
gridControl.Dock = DockStyle.Fill;

view.OptionsDetail.ShowDetailTabs = false;

Controls.Add(gridControl);

Result of example:
示例结果
In additional you can take a look at «How to emulate a TreeList using the master-detail GridView» example.

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