/// <summary>
/// 创建第一个视图
/// </summary>
private void CreateGridView()
{
var grv = this.gridView1;
//创建从表显示的列
grv.Columns.Clear();
grv.Columns.AddVisible("ID", "ID");//.Visible = false;
grv.Columns.AddVisible("Name", "名称");
grv.Columns.AddVisible("Description", "描述内容");
grv.OptionsBehavior.ReadOnly = false;
grv.OptionsBehavior.Editable = true;
}
GridView grv2 = null;
/// <summary>
/// 创建第二个视图
/// </summary>
private void CreateLevelView()
{
var grv = this.gridView1;
var gridControl = this.gridcontrol1;
//创建一个从表的GridView对象
grv2 = new GridView();
grv2.ViewCaption = "记录明细";
grv2.Name = "grv2";
grv2.GridControl = gridControl;
//构建GridLevelNode并添加到LevelTree集合里面
var node = new GridLevelNode();
node.LevelTemplate = grv2;
node.RelationName = "Detail2List";//这里对应集合的属性名称
gridControl.LevelTree.Nodes.AddRange(new GridLevelNode[]
{
node
});
//添加对应的视图集合显示
gridControl.ViewCollection.Clear();
gridControl.ViewCollection.AddRange(new BaseView[] { grv, grv2 });
//创建从表显示的列
grv2.Columns.Clear();
DevExpress.XtraGrid.Columns.GridColumn grid;
grv2.Columns.AddVisible("ID", "ID");
grv2.Columns.AddVisible("Name", "名称");
grv2.Columns.AddVisible("Description", "描述内容");
#region MyRegion
//添加对应的视图集合显示
gridcontrol1.ViewCollection.Clear();
gridcontrol1.ViewCollection.AddRange(new BaseView[] { grv, grv2 });
#endregion
}
public void ss()
{
#region MyRegion
//构建GridLevelNode并添加到LevelTree集合里面
var node = new GridLevelNode();
node.LevelTemplate = grv2;
node.RelationName = "Detail2List";//这里对应集合的属性名称
gridcontrol1.LevelTree.Nodes.AddRange(new GridLevelNode[]
{
node
});
#endregion
}
#region 绑定数据源
/// <summary>
/// 绑定数据源
/// </summary>
private void BindData()
{
//构造一个记录的集合
var list = new List<Detail2Result>();
//创建测试数据
var result = new Detail2Result()
{
Name = "测试",
Description = "描述内容",
Detail2List = new List<DetailInfo>()
{
new DetailInfo()
{
Name = "111测试",
Description = "111描述内容"
},
new DetailInfo()
{
Name = "222测试",
Description = "222描述内容"
},
new DetailInfo()
{
Name = "333测试",
Description = "333描述内容"
}
}
};
list.Add(result);
result = new Detail2Result()
{
Name = "测试2",
Description = "描述内容2",
Detail2List = new List<DetailInfo>()
{
new DetailInfo()
{
Name = "111测试2",
Description = "111描述内容2"
},
new DetailInfo()
{
Name = "222测试2",
Description = "222描述内容2"
},
new DetailInfo()
{
Name = "333测试2",
Description = "333描述内容2"
}
}
};
list.Add(result);
//绑定数据源
this.gridcontrol1.DataSource = list;
}