![](/images/cnblogs_com/king_astar/forwindows/devexpress/devexpress_MasterDetail1.gif)
xtraGrid 本身支持DataSet master/detail的数据直接帮定.可以参看其文档,
当然也可以继承接口:
DevExpress.Data.IRelationList 定义自己的数据源.
还有可以动态来帮定detail的数据.- 通过事件触发的方式.
根据今天我的探索,写一点动态帮定经验.
我有两个实体类
1. orderItem 继承了myCollectionBase(来之于CollectionBase)
2. item (明细类)
[DBTableAttr("SalesOrder")]
public class orderItem : myCollectionBase
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public orderItem()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
public bool FoundFlag
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
get
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.Count > 0)
return true;
else
return false;
}
}
[DBColumnAttr("ordersn")]
public String OrderSn
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderSn; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderSn = value; }
}
[DBColumnAttr("OrderApplyDate")]
public Object OrderApplyDate
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderApplyDate; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderApplyDate = value; }
}
[DBColumnAttr("DeliveryLocation")]
public String DeliveryLocation
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _deliveryLocation; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _deliveryLocation = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
private String _orderSn ="";
private Object _orderApplyDate;
private String _deliveryLocation ="";
//private bool _foundFlag = false;
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
class item#region class item
![](/Images/OutliningIndicators/InBlock.gif)
[DBTableAttr("SalesOrder_Detail")]
public class item
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
[DBColumnAttr("OrderItemId")]
public int OrderItemId
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderItemId; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderItemId = value; }
}
[DBColumnAttr("OrderSn")]
public String OrderSn
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _orderSn; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _orderSn = value; }
}
[DBColumnAttr("ProductMaterialCode")]
public String ProductMaterialCode
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productMaterialCode; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productMaterialCode = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[DBColumnAttr("ProductName")]
public String ProductName
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productName; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productName = value; }
}
[DBColumnAttr("ProductSpec")]
public String ProductSpec
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _productSpec; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _productSpec = value; }
}
[DBColumnAttr("Qty")]
public int Qty
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _qty; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _qty = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
[DBColumnAttr("SuggestionDeliveryDate")]
public Object SuggestionDeliveryDate
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _suggestionDeliveryDate; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _suggestionDeliveryDate = value; }
}
[DBColumnAttr("ConfirmDate")]
public Object ConfirmDate
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _confirmDate; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _confirmDate = value; }
}
[DBColumnAttr("IsConfirm")]
public bool IsConfirm
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _isConfirm; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _isConfirm = value; }
}
[DBColumnAttr("Confirmer")]
public String Confirmer
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _confirmer; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ _confirmer = value; }
}
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
private int _orderItemId;
private String _orderSn;
private String _productMaterialCode;
private String _productName;
private String _productSpec;
private int _qty;
private Object _suggestionDeliveryDate;
private Object _confirmDate;
private bool _isConfirm;
private String _confirmer;
![](/Images/OutliningIndicators/InBlock.gif)
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
}
master的数据源也是一个myCollection对象 : _orders
//获取并帮定master数据源的代码
try
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
orderItem ot = _orderBiz.GetOrderInfoByOrderSn(ordersn, true);
_orders.Add(ot);
//MessageBox.Show(_orders.Count.ToString());
gridControl1.DataSource = _orders;
gridControl1.RefreshDataSource();
![](/Images/OutliningIndicators/InBlock.gif)
}
catch (Exception ex)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
MessageBox.Show(String.Format(" 出错: {0}",ex.Message) );
}
detail数据源则是_orders[i] ,它本身也是一个Collection对象.
先用设计器把想显示的数据列设计好
![](/images/cnblogs_com/king_astar/forwindows/devexpress/devexpress_MasterDetail2.gif)
gridControls 都必然有一个MainView,主视图,你可以通过设计器修改主视图,我就设定gridView1为它的主视图(见上图).
接着通过Create a new level 来创建一个新级别,所有的级别都比MainView至少低一个级别.
我把第二级别定义为orderItems而它使用orderDetail视图来实现数据.
MasterRowGetRelationCount 这个事件只要定义e.RelationCount = 2; 就可以了,这样在界面上才可以看到数据前有+号.
这个事件主要是定义gridView1的关联级别是那一个(注意是orderItems,而不是orderDetail)
private void gridView1_MasterRowGetRelationName(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
e.RelationName = "orderItems";
}
![](/Images/OutliningIndicators/None.gif)
private void gridView1_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[gridView1.GetDataSourceRowIndex(e.RowHandle)];
![](/Images/OutliningIndicators/InBlock.gif)
//为什么不是下面这个?大家考虑一下(提示:排序
.)
// e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[e.RowHandle];
![](/Images/OutliningIndicators/InBlock.gif)
}
private void gridView1_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//没有这个,则+号是虚的,无法点开
e.IsEmpty = false;
}
完成了.运行试试看.