首页 > 代码库 > gridcontrol -- master-detail 绑定数据

gridcontrol -- master-detail 绑定数据

一、绑定DataSet

     1. 创建数据源 DataSet;DataSet中有两张表,并且主表与子表必须建立关联;

             

DataTable dtMaster =new DataTable();DataTable dtDetail= new DataTable();dtMaster .Columns.Add("RelationColumn");dtDetail.Coumns.Add("RelationColumn"); using (DataSet ds = new DataSet())              {                  ds.Tables.AddRange(new DataTable[] {                                                   dtMaster .Copy(),                                                    dtDetail.Copy() 
                              }); // 创建表关系 DataRelation relation = new DataRelation("detailTable", ds.Tables[0].Columns[0], ds.Tables[1].Columns[0], false); ds.Relations.Add(relation); // 添加 gridControl1.DataSource = ds.Tables[0]; // 指定数据源 }

 2. ui 设置 gridControl 控件;

         gridControl中已有一默认的Gridview1 为MainView 用来显示dtMaster的数据;

         gridControl 添加一个level.在该level中创建 gridView 命名为gvDetial. 用来显示dtDetail的数据;

         修改level的名称,名称必须和数据源中定义的DataRelation的名称一致;gridcontrol 会根据MasterTable的relation来创建并显示detail的数据结构;

         将两个Gridveiw中的添加Column ,并绑定相应数据源表的字段;

技术分享
1  this.gvTable.OptionsDetail.AllowExpandEmptyDetails = true;2  this.gvTable.OptionsDetail.ShowDetailTabs = false;3  this.gvTable.OptionsPrint.ExpandAllDetails = true;4  this.gvTable.OptionsPrint.PrintDetails = true;
View Code

 二、绑定实体对象

[DataContract]    [Serializable]    public class PlanInfo:C_ProductPlan    {        public PlanInfo()        {            Items = new List<ProducePlanItem>();        }              [DataMember]        public List<ProducePlanItem> Items { get; set; }    }    [DataContract]    [Serializable]    public class ProducePlanItem :C_ProductPlanItem    {               public ProducePlanItem()        {            ProcessConfig = new List<ProcessInfo>();        }              [DataMember]        public List<ProcessInfo> ProcessConfig { get; set; }                }        [Serializable]    public class ProcessInfo    {        public String Name{get;set;}        public int Order{get;set;}        public bool? IsChecked{get;set;}        public String Memo{get;set;}    }

 

public class ProducePlanItems : ArrayList, IRelationList    {        public ProducePlanItems()        {        }        #region IRelationList 成员        public System.Collections.IList GetDetailList(int index, int relationIndex)        {            return this[index].ProcessConfig;        }        public string GetRelationName(int index, int relationIndex)        {            return "ProcessConfig";        }        public bool IsMasterRowEmpty(int index, int relationIndex)        {            return false;        }        public int RelationCount        {            get { return 1; }        }        #endregion        public virtual new ProducePlanItem this[int index]        {            get { return base[index] as ProducePlanItem; }        }    }

 

 

需要引用Dexexpress.Data.dll, 

 1.gridview-MainView 绑定到UI中定义的ProducePlanItems.

 2.添加一个level1 将level1重命名称ProcessConfig(对应上面IRelationList.GetRelationName的返回
 
 3.level1 添加 gridview2 ,需要手动设置gridview的列,可以设置gridview2的ViewCaption

 4.获取数据后将items转化成producePlanItems,并绑定到UI

gridcontrol -- master-detail 绑定数据