为了解决这个问题搞了一个下午,终于搞定了..这些代码有帮到你们的朋友记得给我留言哦
一,主从表的设置
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
DataTable dt = pb.GetItemInfoList(Port).Copy(); //返回一个TABLE
dt.TableName = "ItemInfo";
ds.Tables.Add(dt);
DataTable dt2 = pb.GetBuildingInfoList(Port).Copy(); //返回一个TABLE
dt2.TableName = "BuildingInfo";
ds.Tables.Add(dt2);
DataColumn keyColumn = ds.Tables["ItemInfo"].Columns["ITEMINFO_ID"];
DataColumn foreignKeyColumn = ds.Tables["BuildingInfo"].Columns["ITEMINFOID"];
ds.Relations.Add("itembuildinginfo", keyColumn, foreignKeyColumn);
gridControl1.DataSource = ds.Tables["ItemInfo"];
dt.TableName = "ItemInfo";
ds.Tables.Add(dt);
DataTable dt2 = pb.GetBuildingInfoList(Port).Copy(); //返回一个TABLE
dt2.TableName = "BuildingInfo";
ds.Tables.Add(dt2);
DataColumn keyColumn = ds.Tables["ItemInfo"].Columns["ITEMINFO_ID"];
DataColumn foreignKeyColumn = ds.Tables["BuildingInfo"].Columns["ITEMINFOID"];
ds.Relations.Add("itembuildinginfo", keyColumn, foreignKeyColumn);
gridControl1.DataSource = ds.Tables["ItemInfo"];
二,获取主表的当前选择行的某一列(如第0列)
三、获取从表的当前选择行的某一列(如ID列)
这个时候再使用获取主表当前选择行的某一列的方法是不行的,因为所得到的seletedrowscount=0。使用如下方法得到:
在MASTER表的展开事件中得到detail有的view.然后就可以利用它了。例:
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
//主表的masterrowexpanded事件
private void gridView1_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
detailView = gridView1.GetDetailView(e.RowHandle, e.RelationIndex) as DevExpress.XtraGrid.Views.Grid.GridView;
}
//取得从表的当前行
int[] i = detailView.GetSelectedRows();
DataRowView dt = (DataRowView)detailView.GetRow(i[0]);
//获得当前行某列的值可以使用
dt["列名"].ToString();
//获得当那个列的值。
private void gridView1_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
{
detailView = gridView1.GetDetailView(e.RowHandle, e.RelationIndex) as DevExpress.XtraGrid.Views.Grid.GridView;
}
//取得从表的当前行
int[] i = detailView.GetSelectedRows();
DataRowView dt = (DataRowView)detailView.GetRow(i[0]);
//获得当前行某列的值可以使用
dt["列名"].ToString();
//获得当那个列的值。