本文介绍如何使用 DevExpress Grid 控件在 Windows Forms 应用中显示图片,并通过 CheckEdit 控件改变单元格的可编辑状态。文章展示了如何为不同的列设置 CheckEdit、ImageComboBox 和 PictureEdit 编辑器。
usingSystem;usingSystem.Drawing;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Windows.Forms;usingSystem.Data;usingSystem.IO;usingDevExpress.XtraGrid.Columns;usingDevExpress.XtraEditors;usingDevExpress.XtraEditors.Controls;usingDevExpress.XtraEditors.Repository;usingDevExpress.Utils.Controls;namespaceImagesInCells{/// <summary>/// Summary description for Form1./// </summary>publicclassForm1:System.Windows.Forms.Form{privateDevExpress.XtraGrid.GridControlgridControl1;privateDevExpress.XtraGrid.Views.Grid.GridViewgridView1;privateDevExpress.XtraEditors.CheckEditcheckEdit1;/// <summary>/// Required designer variable./// </summary>privateSystem.ComponentModel.Containercomponents=null;publicForm1(){//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call//}/// <summary>/// Clean up any resources being used./// </summary>protectedoverridevoidDispose(booldisposing){if(disposing){if(components!=null){components.Dispose();}}base.Dispose(disposing);}#region Windows Form Designer generated code/// <summary>/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// </summary>privatevoidInitializeComponent(){this.gridControl1=newDevExpress.XtraGrid.GridControl();this.gridView1=newDevExpress.XtraGrid.Views.Grid.GridView();this.checkEdit1=newDevExpress.XtraEditors.CheckEdit();((System.ComponentModel.ISupportInitialize)(this.gridControl1)).BeginInit();((System.ComponentModel.ISupportInitialize)(this.gridView1)).BeginInit();((System.ComponentModel.ISupportInitialize)(this.checkEdit1.Properties)).BeginInit();this.SuspendLayout();// // gridControl1// this.gridControl1.Dock=System.Windows.Forms.DockStyle.Fill;this.gridControl1.EmbeddedNavigator.Name="";this.gridControl1.Location=newSystem.Drawing.Point(0, 0);this.gridControl1.MainView=this.gridView1;this.gridControl1.Name="gridControl1";this.gridControl1.Size=newSystem.Drawing.Size(666, 372);this.gridControl1.TabIndex= 0;this.gridControl1.ViewCollection.AddRange(newDevExpress.XtraGrid.Views.Base.BaseView[]{this.gridView1});// // gridView1// this.gridView1.GridControl=this.gridControl1;this.gridView1.Name="gridView1";this.gridView1.OptionsView.ShowGroupPanel=false;// // checkEdit1// this.checkEdit1.Location=newSystem.Drawing.Point(37, 315);this.checkEdit1.Name="checkEdit1";this.checkEdit1.Properties.Caption="Allow Edit";this.checkEdit1.Size=newSystem.Drawing.Size(80, 19);this.checkEdit1.TabIndex= 1;this.checkEdit1.CheckedChanged+=newSystem.EventHandler(this.checkEdit1_CheckedChanged);// // Form1// this.AutoScaleBaseSize=newSystem.Drawing.Size(5, 13);this.ClientSize=newSystem.Drawing.Size(666, 372);this.Controls.Add(this.checkEdit1);this.Controls.Add(this.gridControl1);this.Name="Form1";this.Text="Form1";this.Load+=newSystem.EventHandler(this.Form1_Load);((System.ComponentModel.ISupportInitialize)(this.gridControl1)).EndInit();((System.ComponentModel.ISupportInitialize)(this.gridView1)).EndInit();((System.ComponentModel.ISupportInitialize)(this.checkEdit1.Properties)).EndInit();this.ResumeLayout(false);}#endregion/// <summary>/// The main entry point for the application./// </summary>[STAThread]staticvoidMain(){Application.Run(newForm1());}privateImageGetImageFromResource(stringfileName){Streamresource=typeof(Form1).Assembly.GetManifestResourceStream("ImagesInCells.Resources."+fileName);returnImage.FromStream(resource);}privatebyte[]GetImageData(stringfileName){Imageimg=GetImageFromResource(fileName);MemoryStreammem=newMemoryStream();img.Save(mem,System.Drawing.Imaging.ImageFormat.Bmp);returnmem.GetBuffer();}privatevoidForm1_Load(objectsender,System.EventArgse){DataTabletable=newDataTable();table.Columns.Add("IsRead",typeof(bool));table.Columns.Add("Type",typeof(int));table.Columns.Add("Picture",typeof(byte[]));table.Rows.Add(newobject[]{true, 1,GetImageData("datasource_enabled.bmp")});table.Rows.Add(newobject[]{false, 2,null});table.Rows.Add(newobject[]{null, 3,null});GridColumncolumn;gridView1.PopulateColumns(table);// CheckEditRepositoryItemCheckEditcheckEdit=gridControl1.RepositoryItems.Add("CheckEdit")asRepositoryItemCheckEdit;checkEdit.PictureChecked=GetImageFromResource("read.bmp");checkEdit.PictureUnchecked=GetImageFromResource("unread.bmp");checkEdit.CheckStyle=DevExpress.XtraEditors.Controls.CheckStyles.UserDefined;column=gridView1.Columns["IsRead"];column.ColumnEdit=checkEdit;column.Caption+=" (CheckEdit)";// ImageComboBoxRepositoryItemImageComboBoximageCombo=gridControl1.RepositoryItems.Add("ImageComboBoxEdit")asRepositoryItemImageComboBox;DevExpress.Utils.ImageCollectionimages=newDevExpress.Utils.ImageCollection();images.AddImage(GetImageFromResource("Error.png"));images.AddImage(GetImageFromResource("Warning.png"));images.AddImage(GetImageFromResource("Info.png"));imageCombo.SmallImages=images;imageCombo.Items.Add(newImageComboBoxItem("Error", 1, 0));imageCombo.Items.Add(newImageComboBoxItem("Warning", 2, 1));imageCombo.Items.Add(newImageComboBoxItem("Info", 3, 2));imageCombo.GlyphAlignment=DevExpress.Utils.HorzAlignment.Center;column=gridView1.Columns["Type"];column.ColumnEdit=imageCombo;column.Caption+=" (ImageComboBox)";column.ShowButtonMode=DevExpress.XtraGrid.Views.Base.ShowButtonModeEnum.ShowOnlyInEditor;// PictureEditRepositoryItemPictureEditpictureEdit=gridControl1.RepositoryItems.Add("PictureEdit")asRepositoryItemPictureEdit;pictureEdit.SizeMode=PictureSizeMode.Zoom;pictureEdit.NullText=" ";column=gridView1.Columns["Picture"];column.ColumnEdit=pictureEdit;column.Caption+=" (PictureEdit)";gridControl1.DataSource=table;gridView1.OptionsBehavior.Editable=checkEdit1.Checked;}privatevoidcheckEdit1_CheckedChanged(objectsender,System.EventArgse){gridView1.OptionsBehavior.Editable=checkEdit1.Checked;}}}