• 用嵌套List实现DataGrid的主从表显示


    //首先构造嵌套List,也就是一个list在另一个list中充当成员
    //如:referModels 在res中充当成员
    var res = totalAffectedMedels.Select(c => new
                {
                    TIME_SLICE_ID = c.curModel.TIME_SLICE_ID,
                    referModels = c.referModels == null ? null : c.referModels.Select(n => new
                    {
                        数据类型 = n.TABLE_INFO,
                        名称 = n.NAME,
                        识别 = n.DESIGNATOR,
                        数量 = n.mCount
                    }).ToList(),
                    任务操作 = c.operType == CmmDb.DbOperation.Add ? "添加" :"",
                    数据类型 = c.curModel.TABLE_INFO,
                    名称 = c.curModel.NAME,
                    识别 = c.curModel.DESIGNATOR
                }).ToList();

    //确保GridControl的optionsDetail下的EnableMasterViewMode=true,该属性默认就是true

    //最简单的方法直接将此list绑定到gdc的DataSource就能自动出现主从界面,不用在界面上做任何设置,如
    //要显示隐藏哪些列完全通过上面的自定义List实现
    //子表的标签会自动为referModels
    CmmFrm.BestFitGridViewListWidth(gdc_TaskCancel, res, 1);

    //如果要仅有个别列要隐藏,添加绑定事件MasterRowExpanded,在点击行头的+时触发
    private void gdv_TaskCancel_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
            {
                GridView gridViewMaster = sender as GridView;
                GridView gridViewDetail = gridViewMaster.GetDetailView(e.RowHandle, e.RelationIndex) as GridView;
                if (gridViewDetail.IsNotNull())
                {
                    gridViewDetail.BeginUpdate();
                    gridViewDetail.Columns["TIME_SLICE_ID"].Visible = false;
                    gridViewDetail.Columns["TIME_SLICE_ID"].OptionsColumn.ShowInCustomizationForm = false;
                    gridViewDetail.EndUpdate();
                }
            }

    //一般的做法是在gdv中添加Level视图,点击右键修改视图名称为子List名称,referModels,可绑定子表。所有子表列都能自动显示。
    //也可以在界面中添加字段

    关于子表事件

    子表事件无法直接从界面添加,即使设置了子表界面,只能在主表展开时添加自定义事件,可解决点击子表时获取父表展开行

    private void gdv_refer_MasterRowExpanded(object sender, DevExpress.XtraGrid.Views.Grid.CustomMasterRowEventArgs e)
            {
                if (e.RowHandle >= 0)
                {
                    GridView detailView = gdv_refer.GetDetailView(e.RowHandle, 0) as GridView;
                    detailView.RowCellClick -= GridView_RowCellClick;
                    detailView.RowCellClick += GridView_RowCellClick;
                }
            }

    实现:

    private void GridView_RowCellClick(object sender, RowCellClickEventArgs e)
            {  

    //获取子表当前点击行所在的父表行
                gdv_refer.FocusedRowHandle = gdv_refer.LocateByValue("DETAIL_ID", (sender as GridView).GetFocusedDataRow().GetParentRow("Files")["DETAIL_ID"].ToString());           
            }

  • 相关阅读:
    zabbix监控日志文件
    memached实现tomcat的session共享
    Ansible基础配置与常用模块使用
    ELK之topbeat部署
    ELK之nginx日志分析图表创建
    ELK安装配置及nginx日志分析
    awk将普通文本转换成json文件
    日常工作中使用的一些Mongodb语句
    zabbix监控zookeeper
    zabbix监控mysql
  • 原文地址:https://www.cnblogs.com/mol1995/p/9248235.html
Copyright © 2020-2023  润新知