如果您想使用 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);
示例结果:
另外,您可以查看«How to emulate a TreeList using the master-detail GridView» 示例。