自定义xtraGrid mater/ detail 数据结构显示


xtraGrid 本身支持DataSet master/detail的数据直接帮定.可以参看其文档,
当然也可以继承接口: DevExpress.Data.IRelationList  定义自己的数据源.

还有可以动态来帮定detail的数据.- 通过事件触发的方式.
根据今天我的探索,写一点动态帮定经验.

我有两个实体类
1. orderItem 继承了myCollectionBase(来之于CollectionBase)
2. item (明细类)
None.gif [DBTableAttr( " SalesOrder " )]
None.gif    
public   class  orderItem : myCollectionBase
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public orderItem()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
public bool FoundFlag
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (this.Count > 0)
InBlock.gif                    
return true;
InBlock.gif                
else
InBlock.gif                    
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif      
InBlock.gif        [DBColumnAttr(
"ordersn")]
InBlock.gif        
public String OrderSn
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _orderSn; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _orderSn = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        [DBColumnAttr(
"OrderApplyDate")]
InBlock.gif        
public Object OrderApplyDate
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _orderApplyDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _orderApplyDate = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif        [DBColumnAttr(
"DeliveryLocation")]
InBlock.gif        
public String DeliveryLocation
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
get dot.gifreturn _deliveryLocation; }
ExpandedSubBlockStart.gifContractedSubBlock.gif            
set dot.gif{ _deliveryLocation = value; }
ExpandedSubBlockEnd.gif        }

InBlock.gif   
InBlock.gif
InBlock.gif        
InBlock.gif        
private String _orderSn ="";
InBlock.gif        
private Object _orderApplyDate;
InBlock.gif        
private String _deliveryLocation ="";
InBlock.gif        
//private bool _foundFlag = false;
InBlock.gif

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
class item#region class item
InBlock.gif
InBlock.gif        [DBTableAttr(
"SalesOrder_Detail")]
InBlock.gif        
public class item
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            [DBColumnAttr(
"OrderItemId")]
InBlock.gif            
public int  OrderItemId
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _orderItemId; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _orderItemId = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"OrderSn")]
InBlock.gif            
public String OrderSn
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _orderSn; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _orderSn = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"ProductMaterialCode")]
InBlock.gif            
public String ProductMaterialCode
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _productMaterialCode; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _productMaterialCode = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            [DBColumnAttr(
"ProductName")]
InBlock.gif            
public String ProductName
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _productName; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _productName = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"ProductSpec")]
InBlock.gif            
public String ProductSpec
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _productSpec; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _productSpec = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"Qty")]
InBlock.gif            
public int  Qty
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _qty; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _qty = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            [DBColumnAttr(
"SuggestionDeliveryDate")]
InBlock.gif            
public Object SuggestionDeliveryDate
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _suggestionDeliveryDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _suggestionDeliveryDate = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"ConfirmDate")]
InBlock.gif            
public Object ConfirmDate
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _confirmDate; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _confirmDate = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"IsConfirm")]
InBlock.gif            
public bool  IsConfirm
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _isConfirm; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _isConfirm = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif            [DBColumnAttr(
"Confirmer")]
InBlock.gif            
public String Confirmer
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif                
get dot.gifreturn _confirmer; }
ExpandedSubBlockStart.gifContractedSubBlock.gif                
set dot.gif{ _confirmer = value; }
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            
private int _orderItemId;
InBlock.gif            
private String _orderSn;
InBlock.gif            
private String _productMaterialCode;
InBlock.gif            
private String _productName;
InBlock.gif            
private String _productSpec;
InBlock.gif            
private int _qty;
InBlock.gif            
private Object _suggestionDeliveryDate;
InBlock.gif            
private Object _confirmDate;
InBlock.gif            
private bool _isConfirm;
InBlock.gif            
private String _confirmer;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedBlockEnd.gif    }


master的数据源也是一个myCollection对象 : _orders

None.gif // 获取并帮定master数据源的代码
None.gif
try
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                orderItem ot 
= _orderBiz.GetOrderInfoByOrderSn(ordersn, true);
InBlock.gif                _orders.Add(ot);
InBlock.gif                
//MessageBox.Show(_orders.Count.ToString());
InBlock.gif
                gridControl1.DataSource = _orders;
InBlock.gif                gridControl1.RefreshDataSource();
InBlock.gif                
InBlock.gif
ExpandedBlockEnd.gif            }

None.gif            
catch  (Exception ex)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif
InBlock.gif                MessageBox.Show(String.Format(
" 出错: {0}",ex.Message) );
ExpandedBlockEnd.gif            }


detail数据源则是_orders[i] ,它本身也是一个Collection对象.

先用设计器把想显示的数据列设计好


gridControls 都必然有一个MainView,主视图,你可以通过设计器修改主视图,我就设定gridView1为它的主视图(见上图).

接着通过Create a new level 来创建一个新级别,所有的级别都比MainView至少低一个级别.

我把第二级别定义为orderItems而它使用orderDetail视图来实现数据.

MasterRowGetRelationCount  这个事件只要定义e.RelationCount = 2; 就可以了,这样在界面上才可以看到数据前有+号.

这个事件主要是定义gridView1的关联级别是那一个(注意是orderItems,而不是orderDetail)
None.gif          private   void  gridView1_MasterRowGetRelationName( object  sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetRelationNameEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            e.RelationName 
= "orderItems";
ExpandedBlockEnd.gif        }

None.gif

None.gif   private   void  gridView1_MasterRowGetChildList( object  sender, DevExpress.XtraGrid.Views.Grid.MasterRowGetChildListEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif            e.ChildList 
= (Fiberxon.DataAccess.myCollectionBase)_orders[gridView1.GetDataSourceRowIndex(e.RowHandle)];
InBlock.gif
InBlock.gif
//为什么不是下面这个?大家考虑一下(提示:排序dot.gif.)
InBlock.gif
//   e.ChildList = (Fiberxon.DataAccess.myCollectionBase)_orders[e.RowHandle];
InBlock.gif

ExpandedBlockEnd.gif        }


None.gif private   void  gridView1_MasterRowEmpty( object  sender, DevExpress.XtraGrid.Views.Grid.MasterRowEmptyEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif
//没有这个,则+号是虚的,无法点开
InBlock.gif
            e.IsEmpty = false;
InBlock.gif            
ExpandedBlockEnd.gif        }

完成了.运行试试看.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值