Net Core教程

C# DevExpress GridControl下动态创建列的方法

本文主要是介绍C# DevExpress GridControl下动态创建列的方法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

这里是把在表格中创建控件的方法封装成一个类,然后在创建列的时候实例化“创建控件”的方法

1.在列中使用一些按钮

  1 using Common;
  2 using DevExpress.XtraEditors;
  3 using DevExpress.XtraEditors.Controls;
  4 using DevExpress.XtraEditors.Repository;
  5 using DevExpress.XtraGrid;
  6 using DevExpress.XtraGrid.Views.Grid;
  7 using GyMxErpCs;
  8 using System;
  9 using System.Collections.Generic;
 10 using System.Linq;
 11 using System.Text;
 12 using System.Threading.Tasks;
 13 using System.Windows.Forms;
 14 
 15 namespace Semo
 16 {
 17     public class SheetControl
 18     {
 19         /// <summary>
 20         /// 日期格式控件
 21         /// </summary>
 22         public DevExpress.XtraEditors.Repository.RepositoryItemTextEdit dateTextFormat;
 23         /// <summary>
 24         /// 删除、修改(查看)按钮
 25         /// </summary>
 26         public DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit checkButton;
 27         /// <summary>
 28         /// 超链接控件
 29         /// </summary>
 30         public DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit hyperlinks;
 31         /// <summary>
 32         /// 删除控件(用于明细)
 33         /// </summary>
 34         public DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit deleteButton;
 35         /// <summary>
 36         /// int类型格式控件
 37         /// </summary>
 38         public DevExpress.XtraEditors.Repository.RepositoryItemTextEdit intTextFormat;
 39         /// <summary>
 40         /// 检索控件
 41         /// </summary>
 42         public DevExpress.XtraEditors.Repository.RepositoryItemTextEdit searchText;
 43         /// <summary>
 44         /// 复选框
 45         /// </summary>
 46         public DevExpress.XtraEditors.Repository.RepositoryItemCheckEdit checkbox;
 47 
 48         /// <summary>
 49         /// 下拉控件
 50         /// </summary>
 51         public DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit lookUpEdit;
 52         /// <summary>
 53         /// 文本显示控件
 54         /// </summary>
 55         public DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit memoEdit;
 56         private string strMenuPowerAuth = string.Empty;
 57         private GridControl gridControl;
 58         private object obj;
 59         /// <summary>
 60         /// 
 61         /// </summary>
 62         /// <param name="gridControl">GridControl控件名称</param>
 63         public SheetControl(GridControl gridControl)
 64         {
 65             this.gridControl = gridControl;
 66             InitControl();
 67         }
 68         private void InitControl()
 69         {
 70             #region 日期格式控件
 71             dateTextFormat = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
 72             dateTextFormat.AutoHeight = false;
 73             dateTextFormat.Mask.EditMask = "yyyy-MM-dd";
 74             dateTextFormat.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.DateTime;
 75             dateTextFormat.Mask.UseMaskAsDisplayFormat = true;
 76             dateTextFormat.Name = "dateTextFormat";
 77             #endregion
 78 
 79             #region 删除、查看(修改)
 80             Dictionary<object, string> checkButtons = new Dictionary<object, string>();
 81             if (strMenuPowerAuth.Equals("ck"))
 82             {
 83                 checkButtons.Add("download", "查看");
 84             }
 85             else if (strMenuPowerAuth.Equals("edit"))
 86             {
 87                 checkButtons.Add("download", "修改");
 88                 checkButtons.Add("delete", "删除");
 89             }
 90             checkButton = CreateRepositoryItemButtonEdit(checkButtons);
 91             gridControl.RepositoryItems.Add(checkButton);
 92             #endregion
 93 
 94             #region 明细删除
 95             Dictionary<object, string> delButtons = new Dictionary<object, string>();
 96             delButtons.Add("deletes", "删除");
 97             deleteButton = CreateRepositoryItemButtonEdit(delButtons, 1);
 98             gridControl.RepositoryItems.Add(deleteButton);
 99             #endregion
100 
101             #region 超链接按钮
102             hyperlinks = new DevExpress.XtraEditors.Repository.RepositoryItemHyperLinkEdit();
103             hyperlinks.AutoHeight = false;
104             hyperlinks.Name = "hyperlinks";
105             #endregion
106 
107             #region 检索控件
108             searchText = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
109             searchText.AutoHeight = false;
110             searchText.Name = "searchText";
111             //searchTextEdit.KeyDown += new System.Windows.Forms.KeyEventHandler(this.searchTextEdit_KeyDown);
112             #endregion
113 
114             #region Int格式化控件
115             intTextFormat = new DevExpress.XtraEditors.Repository.RepositoryItemTextEdit();
116             intTextFormat.AutoHeight = false;
117             intTextFormat.Mask.EditMask = "n2";
118             intTextFormat.Mask.MaskType = DevExpress.XtraEditors.Mask.MaskType.Numeric;
119             intTextFormat.Mask.UseMaskAsDisplayFormat = true;
120             intTextFormat.Name = "intTextFormat";
121             #endregion
122 
123             #region 复选框
124             checkbox = new RepositoryItemCheckEdit();
125             #endregion
126 
127             #region 下拉格式控件
128             lookUpEdit = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
129             lookUpEdit.AutoHeight = false;
130             //lookUpEdit.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
131             //new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
132             lookUpEdit.Columns.AddRange(new DevExpress.XtraEditors.Controls.LookUpColumnInfo[] {
133             new DevExpress.XtraEditors.Controls.LookUpColumnInfo("value", "value")});
134             lookUpEdit.Name = "lookUpEdit";
135             lookUpEdit.NullText = "";
136             lookUpEdit.ShowHeader = false;
137             #endregion
138 
139             #region 文本显示控件
140             memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
141             memoEdit.AutoHeight = false;
142             memoEdit.Name = "memoEdit";
143             #endregion
144         }
145         //使用图标
146         //System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmPoductionAddt));
147         //创建按钮
148         private RepositoryItemButtonEdit CreateRepositoryItemButtonEdit(Dictionary<object, string> dicButtons, int ifImage = 0)
149         {
150             RepositoryItemButtonEdit repositoryBtn = new RepositoryItemButtonEdit();
151             repositoryBtn.AutoHeight = false;
152             repositoryBtn.TextEditStyle = TextEditStyles.HideTextEditor;
153             repositoryBtn.ButtonsStyle = BorderStyles.NoBorder;
154             repositoryBtn.Buttons.Clear();
155             EditorButton btn = null;
156             foreach (KeyValuePair<object, string> item in dicButtons)
157             {
158                 btn = new EditorButton();
159                 if (ifImage == 1)
160                 {
161                     btn.Image = ((System.Drawing.Image)(resources.GetObject("repositoryBtn.Buttons")));
162                     btn.ImageLocation = ImageLocation.Default;
163                 }
164                 btn.Kind = ButtonPredefines.Glyph;
165                 btn.Caption = item.Value;
166                 btn.Tag = item.Key;
167                 repositoryBtn.Buttons.Add(btn);
168             }
169             return repositoryBtn;
170         }
171     }
172 }
表格创建中控件

2.创建添加DataTable数据源

 1  //添加表
 2         private void CreateData()
 3         {
 4             //新建一个dt对象
 5             DataTable dt = new DataTable();
 6             dt.Columns.Add("retrieve", Type.GetType("System.String"));//检索
 7             dt.Columns.Add("coding", Type.GetType("System.String"));//编码
 8             dt.Columns.Add("name", Type.GetType("System.String"));//名称
 9             dt.Columns.Add("quantity", Type.GetType("System.Double"));//数量
10             dt.Columns.Add("id", Type.GetType("System.String"));//数量
11             CreateList();//创建列
12             this.gridData.DataSource = dt;
13         }
创建DataTable

3.创建列

 1    //创建列
 2         private void CreateList()
 3         {
 4             SheetControl = new SheetControl(this.gridData);
 5             #region 初始化表头
 6             GridColumn retrieve = new GridColumn();
 7             retrieve.Caption = "操作";
 8             retrieve.FieldName = "retrieve";
 9             retrieve.VisibleIndex = 0;
10             retrieve.ColumnEdit = tableControl.deleteButton;
11             retrieve.Summary.AddRange(new DevExpress.XtraGrid.GridSummaryItem[] {
12             new DevExpress.XtraGrid.GridColumnSummaryItem(DevExpress.Data.SummaryItemType.Average, "retrieve", "合计")});
13             this.gridView1.Columns.Add(retrieve);
14 
15             GridColumn coding = new GridColumn();
16             coding.Caption = "编码";
17             coding.FieldName = "coding";
18             coding.Width = 70;
19             coding.VisibleIndex = 30;
20             coding.ColumnEdit = tableControl.searchText;
21             this.gridView1.Columns.Add(coding);
22 
23             GridColumn name = new GridColumn();
24             name.Caption = "名称";
25             name.FieldName = "name";
26             name.Width = 120;
27             name.VisibleIndex = 32;
28             name.OptionsColumn.AllowEdit = false;
29             name.ColumnEdit = tableControl.memoEdit;
30             name.AppearanceCell.Options.UseTextOptions = true;//自动换行
31             name.AppearanceCell.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;//自动换行
32             this.gridView1.Columns.Add(name);
33 
34             GridColumn id = new GridColumn();
35             id.FieldName = "id";
36             id.Visible = false;
37             this.gridView1.Columns.Add(id);
38             #endregion
39         }
创建列

 

这篇关于C# DevExpress GridControl下动态创建列的方法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!