遗忘海岸

江湖程序员 -Feiph(LM战士)

导航

DevExpress gridcontrol Master-Detail绑定到对象类型

数据库:C_ProductPlan ,C_ProductPlanItem
DTO定义:(实现每个计划条目-Master,对应多个ProcessInfo-Detail)

    [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;}
    }
View Code

 

UI定义:
index-是行记录,relationIndex是对应的子集合序号(可以有多个子集合),IsEmpty=true时主列表前面的"+"灰色
getCount 返回最大的子集合数

   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; }
        }
    }
View Code

需要引用Dexexpress.Data.dll, 

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

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

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

 

//--------------------不采用上面的数据结构设计--------------------------UI上静态设计多级视图------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;

namespace WindowsFormsApplication1
{
    public partial class XtraForm1 : DevExpress.XtraEditors.XtraForm
    {
        public XtraForm1()
        {
            InitializeComponent();
        }

        private void XtraForm1_Load(object sender, EventArgs e)
        {
            #region
            var list = new List<PInfo>();
            for (int i = 0; i < 100; i++)
            {
                var it = new PInfo() { RecId = i, AddTime = DateTime.Now, AddEmpNo = "Admin" };
                it.SubM=new List<MInfo>();
                it.SubList = new List<PInfo>();
                for (int j = 1; j < 10; j++)
                {
                    var sub = new MInfo() { Id = j, Name2 = "name2" + j };
                    it.SubM.Add(sub);
                    var li = new PInfo() { AddEmpNo = "" ,AddTime=DateTime.Now};
                    it.SubList.Add(li);

                }
                list.Add(it);
            }
            this.pInfoBindingSource.DataSource = list;
            #endregion
            var node=gridControl1.LevelTree.Nodes["SubM"];
            var gv = node.LevelTemplate as GridView;
            gv.Columns[0].Caption = "搞成1好不好";

            var node2=gridControl1.LevelTree.Nodes["SubList"];
            var gv2 = node2.LevelTemplate as GridView;
            var c=gv2.Columns["AddTime"];
            c.Caption = "时间";
            c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            c.DisplayFormat.FormatString = "g";
        }

        private void gridControl1_Click(object sender, EventArgs e)
        {
            
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;

namespace WindowsFormsApplication1
{
    public partial class XtraForm2 : DevExpress.XtraEditors.XtraForm
    {
        public XtraForm2()
        {
            InitializeComponent();
        }

        private void XtraForm2_Load(object sender, EventArgs e)
        {
            var gv = new GridView(gridControl1);
            gv.ViewCaption = "列表1";
            var secondNode = new GridLevelNode();
            secondNode.RelationName = "SubList";
            secondNode.LevelTemplate = gv;
            gridControl1.LevelTree.Nodes.Add(secondNode);
            gv.DataSourceChanged +=(s,ee)=>
            {
                var gv1 = s as GridView;
                Console.WriteLine(gv1.Columns.Count);
                var c = gv1.Columns["AddTime"];
                c.Caption = "时间";
                c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                c.DisplayFormat.FormatString = "g";
            };
            #region
            var list = new List<PInfo>();
            for (int i = 0; i < 100; i++)
            {
                var it = new PInfo() { RecId = i, AddTime = DateTime.Now, AddEmpNo = "Admin" };
                it.SubM = new List<MInfo>();
                it.SubList = new List<PInfo>();
                for (int j = 1; j < 10; j++)
                {
                    var sub = new MInfo() { Id = j, Name2 = "name2" + j };
                    it.SubM.Add(sub);
                    var li = new PInfo() { AddEmpNo = "", AddTime = DateTime.Now };
                    it.SubList.Add(li);

                }
                list.Add(it);
            }
            this.pInfoBindingSource.DataSource = list;

            Console.WriteLine(gv.Columns.Count);
            //var c = gv.Columns["AddTime"];
            //c.Caption = "时间";
            //c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            //c.DisplayFormat.FormatString = "g";
            #endregion







            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine(gridControl1.ViewCollection.Count);
            Console.WriteLine(gridControl1.LevelTree.Nodes.Count);
            //var node = gridControl1.LevelTree.Nodes[0];
            //var gv = node.LevelTemplate as GridView;
            //gv.Columns[0].Caption = "搞成1好不好";

            //var node2 = gridControl1.LevelTree.Nodes["SubList"];
            //var gv2 = node2.LevelTemplate as GridView;
            //var c = gv2.Columns["AddTime"];
            //c.Caption = "时间";
            //c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            //c.DisplayFormat.FormatString = "g";
        }
    }
}
View Code

动态生成的gridview,在gridControl.LevelTree.Nodes里是找不到的

对于动态gridview可以在展开ing,或展开ed事件里处理预加载或字段调整

        public XtraForm2()
        {
            InitializeComponent();

            gridView1.MasterRowExpanding += (s, e) =>
            {
                //Console.WriteLine("ing" + e.RowHandle);
                //var view = gridView1.GetDetailView(e.RowHandle, e.RelationIndex);
                //这里获取到的view是null
                //所以适合动态加载数据
                var kb = gridView1.GetRow(e.RowHandle) as PInfo;
                if (kb != null)
                {
                    Console.WriteLine(gridControl1.LevelTree.Nodes.Count);
                    
                }

            };
            gridView1.MasterRowExpanded += (s, e) =>
            {
                var view= gridView1.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
                
                //可以在这里调整view的字段属性
                //注意主节点展开后 多个标签切换时也会产生Expanded事件
                if (Environment.TickCount % 3 == 1)
                {
                    var c = view.Columns["AddTime"];
                    c.Caption = "时间";
                    c.DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
                    c.DisplayFormat.FormatString = "g";
                }
                Console.WriteLine("Expanded");
            };
        }
View Code

 

 

 

参考::https://www.cnblogs.com/wuhuacong/p/8796898.html

posted on 2015-01-26 11:00  遗忘海岸  阅读(2739)  评论(0编辑  收藏  举报