使用Devexpress 的gridControl 绑定List列表(元素对象包含List属性)的Master-Detail显示

1、效果图


2、数据绑定  


        private void DataBinding()
        {
            List students = new List();
            StudentInfo student1 = new StudentInfo() { StudentNo = "0001", Name = "小明", Sex = "男" };
            student1.GradeList.AddRange(new GradeInfo[]{
                new GradeInfo(){ Lesson = "语文", Grade = 85f },
                new GradeInfo(){ Lesson = "数学", Grade = 90f },
                new GradeInfo(){ Lesson = "外语", Grade = 86.5f },
                new GradeInfo(){ Lesson = "物理", Grade = 75f }
            });

            StudentInfo student2 = new StudentInfo() { StudentNo = "0002", Name = "小红", Sex = "女" };
            student2.GradeList.AddRange(new GradeInfo[]{
                new GradeInfo(){ Lesson = "语文", Grade = 90f },
                new GradeInfo(){ Lesson = "数学", Grade = 86f },
                new GradeInfo(){ Lesson = "外语", Grade = 73f }
            });

            students.Add(student1);
            students.Add(student2);
            gridControl1.DataSource = students;
        }
        
        public class StudentInfo
    {
        public string StudentNo { set; get; }

        public string Name { set; get; }

        public string Sex { set; get; }

        private List gradeList = new List();

        public List GradeList
        {
            get { return gradeList; }
            set { gradeList = value; }
        }

    }

    public class GradeInfo
    {
        public string Lesson { set; get; }

        public float Grade { set; get; }
    }public class Records : ArrayList, DevExpress.Data.IRelationList {
    //Adds three items of the Record class 
    public Records() {
        Add(new Record("Customers"));
        Add(new Record("Products"));
        Add(new Record("Shippers"));
    }

    #region  IRelationList methods

    //Returns the name of the relationship depending upon the specified item 
    //The name of the relationship matches the item's name 
    //It is used to determine the pattern View for the relationship 
    string IRelationList.GetRelationName(int index, int relationIndex) {
       if(index != GridControl.InvalidRowHandle)
          return ((Record)this[index]).Name;
       else return "";
    }

    //Returns the number of master-detail relationships for the current data source 
    //Every item is associated with a single relationship 
    int IRelationList.RelationCount {
        get { return 1; }
    }

    //Returns detail data depending upon the current item 
    IList IRelationList.GetDetailList(int index, int relationIndex)    {
        switch(this[index].Name) {
            case "Customers":
                return new ChildRecordsCustomers();
            case "Products":
                return new ChildRecordsProducts();
            case "Shippers":
                return new ChildRecordsShippers();
        }
        return null;
    }

    //Returns whether the specified master row is empty 
    //All master rows provide detail data 
    bool IRelationList.IsMasterRowEmpty(int index, int relationIndex) {
        return false;
    }
    #endregion

    public virtual new Record this[int index] {
        get {return (Record)(base[index]);}
    }
}

//Represents an item for the Records class 
public class Record    {
    private string name;

    public string Name    {
        get { return name;    }
        set { name = value; }
    }

    public Record(string name) {
        this.name = name;
    }
}



        private void DataBinding()
        {
            List students = new List();
            StudentInfo student1 = new StudentInfo() { StudentNo = "0001", Name = "小明", Sex = "男" };
            student1.GradeList.AddRange(new GradeInfo[]{
                new GradeInfo(){ Lesson = "语文", Grade = 85f },
                new GradeInfo(){ Lesson = "数学", Grade = 90f },
                new GradeInfo(){ Lesson = "外语", Grade = 86.5f },
                new GradeInfo(){ Lesson = "物理", Grade = 75f }
            });


            StudentInfo student2 = new StudentInfo() { StudentNo = "0002", Name = "小红", Sex = "女" };
            student2.GradeList.AddRange(new GradeInfo[]{
                new GradeInfo(){ Lesson = "语文", Grade = 90f },
                new GradeInfo(){ Lesson = "数学", Grade = 86f },
                new GradeInfo(){ Lesson = "外语", Grade = 73f }
            });


            students.Add(student1);
            students.Add(student2);
            gridControl1.DataSource = students;
        }
        
        public class StudentInfo
    {
        public string StudentNo { set; get; }


        public string Name { set; get; }


        public string Sex { set; get; }


        private List gradeList = new List();


        public List GradeList
        {
            get { return gradeList; }
            set { gradeList = value; }
        }


    }


    public class GradeInfo
    {
        public string Lesson { set; get; }


        public float Grade { set; get; }
    }


你可能感兴趣的:(c#,ado.net编程)