Devexpress:在GridControl中使用RepositoryItemGridLookUpEdit

     RepositoryItemGridLookUpEdit 用于在GridControl中指定一列为多列数据下拉框,选择的之后只显示指定的列。该Devexpress版本为15.2.

定义名为TestModel类:

 public class TestModel 
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int SizeTypeId { get; set; }
}

定义TestModel集合:

public static List GenerateList()
        {
            return new List() {
                new TestModel() {
                      Id=1,
                      Name=" 桌子",
                },
                new TestModel() {
                     Id=2,
                      Name=" 椅子",
                },
                new TestModel() {
                     Id=3,
                     Name="凳子",
                }
            };
        }

定义RepositoryItemGridLookUpEdit 数据源:

/// 
        /// 生成包含3列的DataTable
        /// 
        public static DataTable GenerateDT()
        {
            DataTable dt = new DataTable("TestTable");
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Type", typeof(string));
            for (int i = 1; i <= 100; i++)
            {
                DataRow rowg = dt.NewRow();
                rowg["Id"] = i;
                rowg["Name"] = $"{i * 12},{i * 10}";
                int size = i * 10 * 10;
                if (size < 3000)
                {
                    rowg["Type"] = "小号";
                }
                if (size >= 3000 && size < 5000)
                {
                    rowg["Type"] = "中号";
                }
                if (size >= 5000)
                {
                    rowg["Type"] = "大号";
                }
                dt.Rows.Add(rowg);
            }
            return dt;
        }

在界面上拖放一个GridControl,窗口加载中定义列

/// 
        /// 数据源
        /// 
        private List list = new List();
        GridColumn gcId = new GridColumn();
        private void GridControlForm_Load(object sender, EventArgs e)
        {
            list.AddRange(GenerateList());
            gv.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
            gcId.Caption = "编号";
            gcId.FieldName = "Id";
            gcId.VisibleIndex = 0;
            gcId.Width = 30;
            this.gv.Columns.Add(gcId);

            GridColumn gcName = gv.Columns.AddField("Name");
            gcName.Caption = "名称";
            gcName.VisibleIndex = 1;
            this.gv.Columns.Add(gcName);
            //尺寸类型 GridLookUpEdit
            RepositoryItemGridLookUpEdit riglup = new RepositoryItemGridLookUpEdit();
            riglup.DataSource = GenerateDT();
            riglup.DisplayMember = "Name";
            riglup.ValueMember = "Id";
            // 为riglup增加3列
            GridColumn riglupgcId = riglup.View.Columns.AddField("Id");
            riglupgcId.Caption = "编号";
            riglupgcId.VisibleIndex = 0;
            riglupgcId.Width = 30;

            GridColumn riglupgcName = riglup.View.Columns.AddField("Name");
            riglupgcName.Caption = "名称";
            riglupgcName.VisibleIndex = 1;

            GridColumn riglupgcType = riglup.View.Columns.AddField("Type");
            riglupgcType.Caption = "尺寸";
            riglupgcType.VisibleIndex = 2;
            // 添加到集合
            riglup.View.Columns.Add(riglupgcId);
            riglup.View.Columns.Add(riglupgcName);
            riglup.View.Columns.Add(riglupgcType);

            GridColumn gcSizeType = gv.Columns.AddField("SizeTypeId");
            gcSizeType.VisibleIndex = 2;
            gcSizeType.Caption = "尺寸";
            gcSizeType.ColumnEdit = riglup;
          
            this.gv.Columns.Add(gcSizeType);
            this.gc.DataSource = list;}

下拉列中自定义列使用如下代码:

GridColumn riglupgcId = riglup.View.Columns.AddField("Id");
            riglupgcId.Caption = "编号";
            riglupgcId.VisibleIndex = 0;
代码运行效果:



你可能感兴趣的:(Devexpress:在GridControl中使用RepositoryItemGridLookUpEdit)