类别:软件操作
日期:2021-02-27 浏览:2683 评论:0
背景
最近公司计划按业务domain拆分系统,原有系统客户端用到DevExpress组件,报表展示都是通过GridControl绑定DataTable实现。考虑到和服务端交互数据的性能问题,在项目升级的同时,想把DataTable替换掉。
GridControl绑定IList<T>比较简单,但是在调试主从表效果时,一直没能达到预期效果,子列表的列名一直显示相应类的字段名称。这个问题令人大伤脑筋,调试过程着实花了一番功夫,但是问题的解决方法却让人大跌眼镜。
考虑到关于这个问题在网上比较难找,所以这里做了个分享,希望对碰到同样问题的朋友有所帮助。
运行效果
定义实体类
/// <summary> /// 学生信息 /// </summary> public class StudentEntity { private int stuId; /// <summary> /// 学号 /// </summary> public int StuId { get { return stuId; } set { stuId = value; } } private string stuName; /// <summary> /// 姓名 /// </summary> public string StuName { get { return stuName; } set { stuName = value; } } private List<StuCourseEntity> courseList; /// <summary> /// 所选课程 /// </summary> public List<StuCourseEntity> CourseList { get { return courseList; } set { courseList = value; } } } /// <summary> /// 学生选课信息 /// </summary> public class StuCourseEntity { private int stuId; /// <summary> /// 学生编号 /// </summary> public int StuId { get { return stuId; } set { stuId = value; } } private int id; /// <summary> /// 课程编号 /// </summary> public int Id { get { return id; } set { id = value; } } private string name; /// <summary> /// 课程名称 /// </summary> public string Name { get { return name; } set { name = value; } } private decimal score; /// <summary> /// 成绩 /// </summary> public decimal Score { get { return score; } set { score = value; } } }
构建学员实例
List<StudentEntity> studentList = new List<StudentEntity>(); studentList.Add(new StudentEntity() { StuId = 101, StuName = "101", CourseList = new List<CourseEntity>() { new CourseEntity(){StuId = 101, Id = 201,Name = "语文", Score = 78}, new CourseEntity(){StuId = 101, Id = 202,Name = "数学", Score = 95}, new CourseEntity(){StuId = 101, Id = 203,Name = "外语", Score = 80} } }); studentList.Add(new StudentEntity() { StuId = 102, StuName = "102", CourseList = new List<CourseEntity>() { new CourseEntity(){StuId = 102, Id = 201,Name = "语文", Score = 80}, new CourseEntity(){StuId = 102, Id = 202,Name = "数学", Score = 95}, new CourseEntity(){StuId = 102, Id = 203,Name = "外语", Score = 80} } }); studentList.Add(new StudentEntity() { StuId = 103, StuName = "103", CourseList = new List<CourseEntity>() { new CourseEntity(){StuId = 103, Id = 201,Name = "语文", Score = 90}, new CourseEntity(){StuId = 103, Id = 202,Name = "数学", Score = 95}, new CourseEntity(){StuId = 103, Id = 203,Name = "外语", Score = 80} } });
绑定List到GridControl gcMain.DataSource = studentList;
GridControl设置
注:这里的LevelName的设置是关键,一定要和Student类的CourseList一致(用DataSet做数据源是主从表关系是通过DataRelation指定的,而IList<T>作为数据源时,这里的关系是通过LevelName对应的,在调试的过程中很容易忽略这个属性的设置)。
发表评论 / 取消回复