自定义xtraGrid mater/ detail 数据结构显示
xtraGrid 本身支持DataSet master/detail的数据直接帮定.可以参看其文档,
当然也可以继承接口:DevExpress.Data.IRelationList 定义自己的数据源.
还有可以动态来帮定detail的数据.- 通过事件触发的方式.
根据今天我的探索,写一点动态帮定经验.
我有两个实体类
1. orderItem 继承了myCollectionBase(来之于CollectionBase)
2. item (明细类)
[DBTableAttr("SalesOrder")]
public class orderItem : myCollectionBase
{
public orderItem()
{
}
public bool FoundFlag
{
get
{
if (this.Count > 0)
return true;
else
return false;
}
}
[DBColumnAttr("ordersn")]
public String OrderSn
{
get { return _orderSn; }
set { _orderSn = value; }
}
[DBColumnAttr("OrderApplyDate")]
public Object OrderApplyDate
{
get { return _orderApplyDate; }
set { _orderApplyDate = value; }
}
[DBColumnAttr("DeliveryLocation")]
public String DeliveryLocation
{
get { return _deliveryLocation; }
set { _deliveryLocation = value; }
}
private String _orderSn ="";
private Object _orderApplyDate;
private String _deliveryLocation ="";
//private bool _foundFlag = false;
class item
}
public class orderItem : myCollectionBase
{
public orderItem()
{
}
public bool FoundFlag
{
get
{
if (this.Count > 0)
return true;
else
return false;
}
}
[DBColumnAttr("ordersn")]
public String OrderSn
{
get { return _orderSn; }
set { _orderSn = value; }
}
[DBColumnAttr("OrderApplyDate")]
public Object OrderApplyDate
{
get { return _orderApplyDate; }
set { _orderApplyDate = value; }
}
[DBColumnAttr("DeliveryLocation")]
public String DeliveryLocation
{
get { return _deliveryLocation; }
set { _deliveryLocation = value; }
}
private String _orderSn ="";
private Object _orderApplyDate;
private String _deliveryLocation ="";
//private bool _foundFlag = false;
class item
}
master的数据源也是一个myCollection对象 : _orders
//获取并帮定master数据源的代码
try
{
orderItem ot = _orderBiz.GetOrderInfoByOrderSn(ordersn, true);
_orders.Add(ot);
//MessageBox.Show(_orders.Count.ToString());
gridControl1.DataSource = _orders;
gridControl1.RefreshDataSource();
}
catch (Exception ex)
{
MessageBox.Show(String.Format(" 出错: {0}",ex.Message) );
}
try
{
orderItem ot = _orderBiz.GetOrderInfoByOrderSn(ordersn, true);
_orders.Add(ot);
//MessageBox.Show(_orders.Count.ToString());
gridControl1.DataSource = _orders;
gridControl1.RefreshDataSource();
}
catch (Exception ex)
{
MessageBox.Show(String.Format(" 出错: {0}",ex.Message) );
}
detail数据源则是_orders[i] ,它本身也是一个Collection对象.
先用设计器把想显示的数据列设计好
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)
{
e.RelationName = "orderItems";
}
{
e.RelationName = "orderItems";
}
private void gridView1_MasterRowGetChildList(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
{
e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[gridView1.GetDataSourceRowIndex(e.RowHandle)];
//为什么不是下面这个?大家考虑一下(提示:排序.)
// e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[e.RowHandle];
}
{
e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[gridView1.GetDataSourceRowIndex(e.RowHandle)];
//为什么不是下面这个?大家考虑一下(提示:排序.)
// e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[e.RowHandle];
}
private void gridView1_MasterRowEmpty(object sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
{
//没有这个,则+号是虚的,无法点开
e.IsEmpty = false;
}
{
//没有这个,则+号是虚的,无法点开
e.IsEmpty = false;
}
完成了.运行试试看.