Devexpress:在GridControl中使用RepositoryItemGridLookUpEdit
阿新 • • 發佈:2019-02-11
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<TestModel> GenerateList() { return new List<TestModel>() { new TestModel() { Id=1, Name=" 桌子", }, new TestModel() { Id=2, Name=" 椅子", }, new TestModel() { Id=3, Name="凳子", } }; }
定義RepositoryItemGridLookUpEdit 資料來源:
/// <summary> /// 生成包含3列的DataTable /// </summary> 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,視窗載入中定義列:
/// <summary> /// 資料來源 /// </summary> private List<TestModel> list = new List<TestModel>(); 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;