• XtraGrid控件-----List数据源实现主从表关系绑定


    一、实现方式

    1、准备源数据。

    /// <summary>
    /// 记录基础信息
    /// </summary>
    public class DetailInfo
    {
        public DetailInfo()
        {
            this.ID = Guid.NewGuid().ToString();
        }
    
        /// <summary>
        /// ID标识
        /// </summary>
        public string ID { get; set; }
    
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
    
        /// <summary>
        /// 描述信息
        /// </summary>
        public string Description { get; set; }
    }
    
    /// <summary>
    /// 二级层次的列表
    /// </summary>
    public class Detail2Result : DetailInfo
    {
        public List<DetailInfo> Detail2List { get; set; }
    }

    2、绑定数据源

    /// <summary>
    /// 绑定数据源
    /// </summary>
    private void BindData()
    {
        //创建测试数据
        var result1 = new Detail2Result()
        {
            Name = "测试1",
            Description = "描述内容",
            Detail2List = new List<DetailInfo>()
            {
                new DetailInfo()
                {
                    Name = "111测试",
                    Description = "111描述内容"
                },
                new DetailInfo()
                {
                    Name = "222测试",
                    Description = "222描述内容"
                },
                new DetailInfo()
                {
                    Name = "333测试",
                    Description = "333描述内容"
                }
            }
        };
    
        var result2 = new Detail2Result()
        {
            Name = "测试2",
            Description = "描述内容",
            Detail2List = new List<DetailInfo>()
            {
                new DetailInfo()
                {
                    Name = "111测试",
                    Description = "111描述内容"
                },
                new DetailInfo()
                {
                    Name = "222测试",
                    Description = "222描述内容"
                },
                new DetailInfo()
                {
                    Name = "333测试",
                    Description = "333描述内容"
                }
            }
        };
    
        //构造一个记录的集合
        var list = new List<Detail2Result>() { result1, result2 };
    
        //绑定数据源
        this.gridControl.DataSource = list;
    }

    3、DevExpress的GridControl控件的设置

    在主视图下添加一层视图为gridView2,并修改当前视图的 Change LeveName 为Detail2List (如果不修改结果是数据绑定失败)

    image

    设置gridView2属性中的ViewCaption 属性。

    image

     

    4、效果

    image 

    5、子表格获取行数据

    DevExpress.XtraGrid.Views.Grid.GridView currentView = (DevExpress.XtraGrid.Views.Grid.GridView)this.gridControl.FocusedView;
    DetailInfo focusRow = currentView.GetFocusedRow() as DetailInfo; //用在事件中:currentView.GetRow(e.RowHandle) as DetailInfo;
    XtraMessageBox.Show(focusRow.Name);
  • 相关阅读:
    Spring Boot启用Swagger2
    Springboot 注解最全详解
    spring-boot-starter-data-jpa 解析
    springboot 微信支付
    springboot整合PageHelper
    SpringBoot配置HTTPS,并实现HTTP访问自动转HTTPS访问
    Springboot 发送短信验证码
    Java volatile关键字的作用
    Android兼容性测试应该怎么做逼格更高呢?
    hadoop日志【2】
  • 原文地址:https://www.cnblogs.com/springsnow/p/13438831.html
Copyright © 2020-2023  润新知