嵌套表格效果图一
嵌套表格效果图二
可视化编辑界面,一个gridControl1(GridControl),下面是MainView(名为gridView_dept的GridView),再添加子层Level1(也是个GridView,名为gridView_Module)
编辑两个GridView的各个Columns,把FieldName字段与数据库表的名称对应好
代码:
public DataSet ds = new DataSet();
public DataTable dtModule;
public DataTable dtDept;
private void onLoad(object sender, EventArgs e)
{
dtDept = BLLUtil.GetTableData(TableField.T_FORM.T_NAME, string.Empty);//父表装进DataTable
dtModule = BLLUtil.GetTableData(TableField.T_FORM_QUESTION.T_NAME, string.Empty);//子表装进DataTable
ds.Tables.Add(dtDept);
ds.Tables.Add(dtModule);
//下面映射主从关系Add(Level1是名称,会显示在子表的标签处,有文章说这个名称要与子一级的GridView名称相同,我试过不同也可以,比如这里写Level2也没问题))
//第二个参数是主表的对应键
//第三个参数是子表的对应键
ds.Relations.Add("Level1",ds.Tables[TableField.T_FORM.T_NAME].Columns["FORMID"],ds.Tables[TableField.T_FORM_QUESTION.T_NAME].Columns["FORMID"]);
this.gridControl1.DataSource = ds.Tables[0];
}
运行就可以了。
P.S.1:ChildGridViewName的属性我没设置。也没问题。
P.S.2:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
这个错误惭愧了,没有把DataSet ds初始化就Add。
P.S.3:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.ArgumentException: This constraint cannot be enabled as not all values have corresponding parent values.
这个错误是数据问题,这个嵌套表格对于数据要求是“只许父无子,不能子无父”,如果子表的映射键值,在父表中没有,则报此错误。