Devexpress:在GridControl中使用RepositoryItemGridLookUpEdit 进行级联选择

上一篇点击打开链接中描述了如何在GridControl中增加一个GridLookUpEdit列,这篇文章将要描述如何在它单元格数据之间进行级联选择。

为GridControl增加一个颜色列,根据不同尺寸的大小来切换选择不同的颜色。效果图如下:



为TestModel类增加ColorId属性:

 public int ColorId { get; set; }

定义名为ColorClass的类:

 public class ColorClass
    {
        public int Id { get; set; }
        public string ColorName { get; set; }
    }


定义2个返回IEnumeable 集合的方法:

        /// 
        /// 小号对应的可选颜色
        /// 
        public static IEnumerable GenerateColorList1()
        {
            yield return new ColorClass() { Id = 1, ColorName = "红色" };
            yield return new ColorClass() { Id = 2, ColorName = "蓝色" };
        }
        /// 
        /// 中号,大号对应的可选颜色
        /// 
        public static IEnumerable GenerateColorList2()
        {
            yield return new ColorClass() { Id = 3, ColorName = "白色" };
            yield return new ColorClass() { Id = 4, ColorName = "黑色" };
        }


 
  

再增加一个表示颜色的GridColumn:

this.gv.Columns.Add(gcSizeType);
            // 尺寸选择改变,清空gcColorType列显示
            riglup.EditValueChanged += (m, n) =>
            {
                this.gv.SetFocusedRowCellValue("ColorId", 0);
            };

            // 颜色类型  GridLookUpEdit
            RepositoryItemGridLookUpEdit riglupcolor1 = new RepositoryItemGridLookUpEdit();
            riglupcolor1.DisplayMember = "ColorName";
            riglupcolor1.ValueMember = "Id";

            RepositoryItemGridLookUpEdit riglupcolor2 = new RepositoryItemGridLookUpEdit();
            riglupcolor2.DisplayMember = "ColorName";
            riglupcolor2.ValueMember = "Id";


            // 为riglupcolor1/riglupcolor2 增加2列
            GridColumn riglupcolor1Id = riglupcolor1.View.Columns.AddField("Id");
            riglupcolor1Id.Caption = "编号";
            riglupcolor1Id.VisibleIndex = 0;
            riglupcolor1Id.Width = 30;
            GridColumn riglupcolor1Name = riglupcolor1.View.Columns.AddField("ColorName");
            riglupcolor1Name.Caption = "名称";
            riglupcolor1Name.VisibleIndex = 1;
            riglupcolor1Name.Width = 30;

            GridColumn riglupcolor2Id = riglupcolor2.View.Columns.AddField("Id");
            riglupcolor2Id.Caption = "编号";
            riglupcolor2Id.VisibleIndex = 0;
            riglupcolor2Id.Width = 30;
            GridColumn riglupcolor2Name = riglupcolor2.View.Columns.AddField("ColorName");
            riglupcolor2Name.Caption = "名称";
            riglupcolor2Name.VisibleIndex = 1;
            riglupcolor2Name.Width = 30;
            // 添加到riglupcolor1/riglupcolor2的Columns集合
            riglupcolor1.View.Columns.Add(riglupcolor1Id);
            riglupcolor1.View.Columns.Add(riglupcolor1Name);
            riglupcolor2.View.Columns.Add(riglupcolor2Id);
            riglupcolor2.View.Columns.Add(riglupcolor2Name);
            //定义颜色列
            GridColumn gcColorType = gv.Columns.AddField("ColorId");
            gcColorType.Caption = "颜色";
            gcColorType.VisibleIndex = 3;
            gv.Columns.Add(gcColorType);
            // 自定义单元格编辑事件
            gv.CustomRowCellEdit += (s, e1) =>
            {
                if (e1.Column.FieldName == "ColorId" && e1.RowHandle >= 0)
                {
                    // 获取尺寸类型
                    string sizeTypeId = gv.GetRowCellValue(e1.RowHandle, "SizeTypeId").ToString();
                    RepositoryItemGridLookUpEdit riglu = gcSizeType.ColumnEdit as RepositoryItemGridLookUpEdit;
                    DataRow dr = (riglu.DataSource as DataTable).Select($" Id = {sizeTypeId}").SingleOrDefault();
                    string name = dr?["Name"].ToString();
                    // 根据尺寸类型显示不同的颜色
                    if (name == "小号")
                    {
                        riglupcolor1.DataSource = DataHelper.GenerateColorList1().ToList();
                        e1.RepositoryItem = riglupcolor1;
                    }
                    else
                    {
                        riglupcolor2.DataSource = DataHelper.GenerateColorList2().ToList();
                        e1.RepositoryItem = riglupcolor2;
                    }
                }
            };
            this.gc.DataSource = list;

代码中的DataHelper类为一个包含GenerateColorList1与GenerateColorList2方法的静态类。

总结:掌握常用的几种RepositoryItem类型的用法,这种级联效果很容易能做的出来,不过需要花费大量的时间去官网上查询。

你可能感兴趣的:(Devexpress:在GridControl中使用RepositoryItemGridLookUpEdit 进行级联选择)