//首先构造嵌套List,也就是一个list在另一个list中充当成员
//如:referModels 在res中充当成员
var res = totalAffectedMedels.Select(c => new
{
TIME_SLICE_ID = c.curModel.TIME_SLICE_ID,
referModels = c.referModels == null ? null : c.referModels.Select(n => new
{
数据类型 = n.TABLE_INFO,
名称 = n.NAME,
识别 = n.DESIGNATOR,
数量 = n.mCount
}).ToList(),
任务操作 = c.operType == CmmDb.DbOperation.Add ? "添加" :"",
数据类型 = c.curModel.TABLE_INFO,
名称 = c.curModel.NAME,
识别 = c.curModel.DESIGNATOR
}).ToList();
//确保GridControl的optionsDetail下的EnableMasterViewMode=true,该属性默认就是true
//最简单的方法直接将此list绑定到gdc的DataSource就能自动出现主从界面,不用在界面上做任何设置,如
//要显示隐藏哪些列完全通过上面的自定义List实现
//子表的标签会自动为referModels
CmmFrm.BestFitGridViewListWidth(gdc_TaskCancel, res, 1);
//如果要仅有个别列要隐藏,添加绑定事件MasterRowExpanded,在点击行头的+时触发
private void gdv_TaskCancel_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
GridView gridViewMaster = sender as GridView;
GridView gridViewDetail = gridViewMaster.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
if (gridViewDetail.IsNotNull())
{
gridViewDetail.BeginUpdate();
gridViewDetail.Columns["TIME_SLICE_ID"].Visible = false;
gridViewDetail.Columns["TIME_SLICE_ID"].OptionsColumn.ShowInCustomizationForm = false;
gridViewDetail.EndUpdate();
}
}
//一般的做法是在gdv中添加Level视图,点击右键修改视图名称为子List名称,referModels,可绑定子表。所有子表列都能自动显示。
//也可以在界面中添加字段
关于子表事件
子表事件无法直接从界面添加,即使设置了子表界面,只能在主表展开时添加自定义事件,可解决点击子表时获取父表展开行
private void gdv_refer_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
if (e.RowHandle >= 0)
{
GridView detailView = gdv_refer.GetDetailView(e.RowHandle, 0) as GridView;
detailView.RowCellClick -= GridView_RowCellClick;
detailView.RowCellClick += GridView_RowCellClick;
}
}
实现:
private void GridView_RowCellClick(object sender, RowCellClickEventArgs e)
{
//获取子表当前点击行所在的父表行
gdv_refer.FocusedRowHandle = gdv_refer.LocateByValue("DETAIL_ID", (sender as GridView).GetFocusedDataRow().GetParentRow("Files")["DETAIL_ID"].ToString());
}