GridControl填充复选框列表项

 第三方控件DevExpress功能挺强大。不过,面对太多的属性和方法,用起来真的感觉如大海捞针。这2天,项目上修整界面,想在GridControl加一列复选框列表项。结果,尝试了N多方法,翻了一堆错误,还是没整出来。最后,还是项目老大帮我解决了。无语!!!2个小错误把我折腾的死去活来。废话少说上代码吧。

  

  XtraGridDemo.cs代码:

  1. public partial class XtraGridDemo : DevExpress.XtraEditors.XtraForm  
  2. {  
  3.     private DataTable dt;  
  4.   
  5.     public XtraGridDemo()  
  6.     {  
  7.         InitializeComponent();  
  8.     }  
  9.   
  10.     private void XtraGridDemo_Load(object sender, EventArgs e)  
  11.     {  
  12.         dt = new DataTable();  
  13.         dt.Columns.Add("ID"typeof(int));  
  14.         dt.Columns.Add("Name"typeof(string));  
  15.         dt.Columns.Add("Items"typeof(string));  
  16.   
  17.         DataRow dr = dt.NewRow();  
  18.         dr[0] = 1;              
  19.         dr[1] = "Allen";  
  20.         dr[2] = "项目一";  
  21.         dt.Rows.Add(dr);  
  22.   
  23.         dr = dt.NewRow();  
  24.         dr[0] = 2;  
  25.         dr[1] = "Jack";  
  26.         dr[2] = "项目一;项目二";  
  27.         dt.Rows.Add(dr);  
  28.   
  29.         dr = dt.NewRow();  
  30.         dr[0] = 3;  
  31.         dr[1] = "Mike";  
  32.         dr[2] = "项目一;项目二;项目三";  
  33.         dt.Rows.Add(dr);  
  34.   
  35.         this.gridControl1.DataSource = dt;  
  36.   
  37.         //定义表项分割符,默认是','   
  38.         this.repositoryItemCheckedComboBoxEdit1.SeparatorChar = ';';  
  39.   
  40.         this.repositoryItemCheckedComboBoxEdit1.Items.Add(new CheckedListBoxItem("项目一"false));  
  41.         this.repositoryItemCheckedComboBoxEdit1.Items.Add(new CheckedListBoxItem("项目二"false));  
  42.         this.repositoryItemCheckedComboBoxEdit1.Items.Add(new CheckedListBoxItem("项目三"false));  
  43.     }  
  44. }  
    public partial class XtraGridDemo : DevExpress.XtraEditors.XtraForm
    {
        private DataTable dt;

        public XtraGridDemo()
        {
            InitializeComponent();
        }

        private void XtraGridDemo_Load(object sender, EventArgs e)
        {
            dt = new DataTable();
            dt.Columns.Add("ID", typeof(int));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Items", typeof(string));

            DataRow dr = dt.NewRow();
            dr[0] = 1;            
            dr[1] = "Allen";
            dr[2] = "项目一";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 2;
            dr[1] = "Jack";
            dr[2] = "项目一;项目二";
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr[0] = 3;
            dr[1] = "Mike";
            dr[2] = "项目一;项目二;项目三";
            dt.Rows.Add(dr);

            this.gridControl1.DataSource = dt;

            //定义表项分割符,默认是','
            this.repositoryItemCheckedComboBoxEdit1.SeparatorChar = ';';

            this.repositoryItemCheckedComboBoxEdit1.Items.Add(new CheckedListBoxItem("项目一", false));
            this.repositoryItemCheckedComboBoxEdit1.Items.Add(new CheckedListBoxItem("项目二", false));
            this.repositoryItemCheckedComboBoxEdit1.Items.Add(new CheckedListBoxItem("项目三", false));
        }
    }

  XtraGridDemo.Designer.cs代码

  1. /// <summary>   
  2. /// Required method for Designer support - do not modify   
  3. /// the contents of this method with the code editor.   
  4. /// </summary>   
  5. private void InitializeComponent()  
  6. {  
  7.     this.gridControl1 = new DevExpress.XtraGrid.GridControl();  
  8.     this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();  
  9.     this.gcId = new DevExpress.XtraGrid.Columns.GridColumn();  
  10.     this.gcName = new DevExpress.XtraGrid.Columns.GridColumn();  
  11.     this.gcItems = new DevExpress.XtraGrid.Columns.GridColumn();  
  12.     this.repositoryItemCheckedComboBoxEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemCheckedComboBoxEdit();  
  13.     ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();  
  14.     ((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();  
  15.     ((System.ComponentModel.ISupportInitialize)(this.repositoryItemCheckedComboBoxEdit1)).BeginInit();  
  16.     this.SuspendLayout();  
  17.     //    
  18.     // gridControl1   
  19.     //    
  20.     this.gridControl1.Location = new System.Drawing.Point(0, 2);  
  21.     this.gridControl1.MainView = this.gridView1;  
  22.     this.gridControl1.Name = "gridControl1";  
  23.     this.gridControl1.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {  
  24.     this.repositoryItemCheckedComboBoxEdit1});  
  25.     this.gridControl1.Size = new System.Drawing.Size(585, 402);  
  26.     this.gridControl1.TabIndex = 0;  
  27.     this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {  
  28.     this.gridView1});  
  29.     //    
  30.     // gridView1   
  31.     //    
  32.     this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {  
  33.     this.gcId,  
  34.     this.gcName,  
  35.     this.gcItems});  
  36.     this.gridView1.GridControl = this.gridControl1;  
  37.     this.gridView1.Name = "gridView1";  
  38.     //    
  39.     // gcId   
  40.     //    
  41.     this.gcId.Caption = "学号";  
  42.     this.gcId.FieldName = "ID";  
  43.     this.gcId.Name = "gcId";  
  44.     this.gcId.Visible = true;  
  45.     this.gcId.VisibleIndex = 0;  
  46.     //    
  47.     // gcName   
  48.     //    
  49.     this.gcName.Caption = "姓名";  
  50.     this.gcName.FieldName = "Name";  
  51.     this.gcName.Name = "gcName";  
  52.     this.gcName.Visible = true;  
  53.     this.gcName.VisibleIndex = 1;  
  54.     //    
  55.     // gcItems   
  56.     //    
  57.     this.gcItems.Caption = "科目";  
  58.     this.gcItems.ColumnEdit = this.repositoryItemCheckedComboBoxEdit1;  
  59.     this.gcItems.FieldName = "Items";  
  60.     this.gcItems.Name = "gcItems";  
  61.     this.gcItems.Visible = true;  
  62.     this.gcItems.VisibleIndex = 2;  
  63.     //    
  64.     // repositoryItemCheckedComboBoxEdit1   
  65.     //    
  66.     this.repositoryItemCheckedComboBoxEdit1.AutoHeight = false;  
  67.     this.repositoryItemCheckedComboBoxEdit1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {  
  68.     new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});  
  69.     this.repositoryItemCheckedComboBoxEdit1.Name = "repositoryItemCheckedComboBoxEdit1";  
  70.     //    
  71.     // XtraGridDemo   
  72.     //    
  73.     this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);  
  74.     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  75.     this.ClientSize = new System.Drawing.Size(585, 404);  
  76.     this.Controls.Add(this.gridControl1);  
  77.     this.Name = "XtraGridDemo";  
  78.     this.Text = "GridControl填充复选框列表项";  
  79.     this.Load += new System.EventHandler(this.XtraGridDemo_Load);  
  80.     ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();  
  81.     ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();  
  82.     ((System.ComponentModel.ISupportInitialize)(this.repositoryItemCheckedComboBoxEdit1)).EndInit();  
  83.     this.ResumeLayout(false);  
  84. }  
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.gridControl1 = new DevExpress.XtraGrid.GridControl();
            this.gridView1 = new DevExpress.XtraGrid.Views.Grid.GridView();
            this.gcId = new DevExpress.XtraGrid.Columns.GridColumn();
            this.gcName = new DevExpress.XtraGrid.Columns.GridColumn();
            this.gcItems = new DevExpress.XtraGrid.Columns.GridColumn();
            this.repositoryItemCheckedComboBoxEdit1 = new DevExpress.XtraEditors.Repository.RepositoryItemCheckedComboBoxEdit();
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemCheckedComboBoxEdit1)).BeginInit();
            this.SuspendLayout();
            // 
            // gridControl1
            // 
            this.gridControl1.Location = new System.Drawing.Point(0, 2);
            this.gridControl1.MainView = this.gridView1;
            this.gridControl1.Name = "gridControl1";
            this.gridControl1.RepositoryItems.AddRange(new DevExpress.XtraEditors.Repository.RepositoryItem[] {
            this.repositoryItemCheckedComboBoxEdit1});
            this.gridControl1.Size = new System.Drawing.Size(585, 402);
            this.gridControl1.TabIndex = 0;
            this.gridControl1.ViewCollection.AddRange(new DevExpress.XtraGrid.Views.Base.BaseView[] {
            this.gridView1});
            // 
            // gridView1
            // 
            this.gridView1.Columns.AddRange(new DevExpress.XtraGrid.Columns.GridColumn[] {
            this.gcId,
            this.gcName,
            this.gcItems});
            this.gridView1.GridControl = this.gridControl1;
            this.gridView1.Name = "gridView1";
            // 
            // gcId
            // 
            this.gcId.Caption = "学号";
            this.gcId.FieldName = "ID";
            this.gcId.Name = "gcId";
            this.gcId.Visible = true;
            this.gcId.VisibleIndex = 0;
            // 
            // gcName
            // 
            this.gcName.Caption = "姓名";
            this.gcName.FieldName = "Name";
            this.gcName.Name = "gcName";
            this.gcName.Visible = true;
            this.gcName.VisibleIndex = 1;
            // 
            // gcItems
            // 
            this.gcItems.Caption = "科目";
            this.gcItems.ColumnEdit = this.repositoryItemCheckedComboBoxEdit1;
            this.gcItems.FieldName = "Items";
            this.gcItems.Name = "gcItems";
            this.gcItems.Visible = true;
            this.gcItems.VisibleIndex = 2;
            // 
            // repositoryItemCheckedComboBoxEdit1
            // 
            this.repositoryItemCheckedComboBoxEdit1.AutoHeight = false;
            this.repositoryItemCheckedComboBoxEdit1.Buttons.AddRange(new DevExpress.XtraEditors.Controls.EditorButton[] {
            new DevExpress.XtraEditors.Controls.EditorButton(DevExpress.XtraEditors.Controls.ButtonPredefines.Combo)});
            this.repositoryItemCheckedComboBoxEdit1.Name = "repositoryItemCheckedComboBoxEdit1";
            // 
            // XtraGridDemo
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 14F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(585, 404);
            this.Controls.Add(this.gridControl1);
            this.Name = "XtraGridDemo";
            this.Text = "GridControl填充复选框列表项";
            this.Load += new System.EventHandler(this.XtraGridDemo_Load);
            ((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.repositoryItemCheckedComboBoxEdit1)).EndInit();
            this.ResumeLayout(false);
        }

DevExpressXtraGridDemo源代码下载

原文地址:https://www.cnblogs.com/lteal/p/2836974.html