使用WinForms DevExpress XtraGrid显示层次结构树c#

时间:2014-07-08 08:38:49

标签: c# winforms tree devexpress

我有分层数据结构,包括以下对象:

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

现在我需要在某种可视树中显示这种层次结构。 我正在使用WinForms C#/你可以请教我如何像这样显示树。 Standart TreeView没有DataSource属性,DevExpress XtraTreeView使用带有Id / ParentID关系的DataSource plain source。我使用过XtraGrid,但我无法正确配置详细信息视图。你能告诉我以哪种方式处理这个问题吗?

UPD

在白天,duscussions诞生决定TreeList不适合我们。因为我们需要使用auttrailter row和grouppanel(我可以拖动列进行分组)来提供XtraGrid。 因此,如果没有使用XtraGrid实现这种树的解决方案,我应该关闭这个问题。

3 个答案:

答案 0 :(得分:4)

您可以使用TreeList.IVirtualTreeListData界面 这是一个例子:

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;
        }
    }
}

示例的用法:

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

答案 1 :(得分:1)

有两种建议的方法可以将业务对象绑定到DevExpress TreeList:

1)使对象适应TreeList控件中的显示。在这种情况下,对象必须实现DevExpress.XtraTreeList.IVirtualTreeListData接口。有关详细信息,请参阅this article 示例:How to: Implement a Tree Structure for a Business Object

2)处理特定的TreeList事件并为根节点和子节点提供数据。 有关详细信息,请参阅this article 示例:How to: Load Data Dynamically via Events

答案 2 :(得分:1)

如果要将XtraGrid用于层次结构,则可以使用IRelationList接口 这是一个例子:

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; }
    }
}

示例的用法:

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
另外,您可以查看«How to emulate a TreeList using the master-detail GridView»示例。