Devexpress GridControl主从表设置不同视图并展开(数据源为DataTable)
1.先获取主从表的数据,并在dataset中指定主从表的关系Relations。
product为主表,productde为从表。
DataSet dt = new DataSet();
private void Grid_Load(object sender, EventArgs e)
{
SqlConnection main=GetSqlConnection();
new SqlDataAdapter("SELECT p_code,p_name,p_dw,size,color FROM product WHERE LEN(p_code)=3", main).Fill(dt,"product");
new SqlDataAdapter("SELECT SUBSTRING(p_code,1,3)'fl',p_code,p_name,p_dw,size,color FROM product WHERE LEN(p_code)=6", main).Fill(dt, "productde");
dt.Relations.Add("详细信息", dt.Tables[0].Columns["p_code"], dt.Tables[1].Columns["fl"]);
gridControl1.DataSource = dt.Tables[0];
product.SetMasterRowExpanded(0, true);
}
2.在主视图的gridView1_MasterRowGetRelationName事件中将e.RelationName = "level1"属性设置为指定的子视图级别名称即可展开子表时按对应的视图展开。
这里我主表使用girdview进行显示,子表使用layoutview进行展开。
private void gridView1_MasterRowGetRelationName(object sender, MasterRowGetRelationNameEventArgs e)
{
e.RelationName = "level1";
}