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