include a image in devexpress datagrid

  • Add an ImageCollection to yout form and add some icons 16x16 to it.
  • Add a column to the Grid for the icons.
  • Set the column's fieldName to image (whatever you like).
  • Set the column's UnboundType to Object.
  • Add a repositoryItemPictureEdit to the column's columnEdit.

All the above can be done in the designer. Then do the following

privatevoid gridView1_CustomUnboundColumnData(object sender,DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e){if(e.Column== colImage1 && e.IsGetData){string someValueFromDatabase =(string)gridView1.GetRowCellValue(e.RowHandle, colOne);if(someValueFromDatabase =="a"){//Set an icon with index 0 e.Value= imageCollection1.Images(0);}else{//Set an icon with index 1 e.Value= imageCollection1.Images(1);}}}

The key here is handling the CustomUnboundColumnData and the repositoryItemPictureEdit.

 

***

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using DevExpress.XtraEditors.Repository;



namespace WindowsApplication1

{

    public partial class Form1 : Form

    {

        private DataTable CreateTable(int RowCount)

        {

            Image[] images = new Image[] { WindowsApplication1.Properties.Resources.about, WindowsApplication1.Properties.Resources.add, WindowsApplication1.Properties.Resources.apple, WindowsApplication1.Properties.Resources.arrow_down, WindowsApplication1.Properties.Resources.arrow_left};

            DataTable tbl = new DataTable();

            tbl.Columns.Add("Name", typeof(string));

            tbl.Columns.Add("ID", typeof(int));

            tbl.Columns.Add("Number", typeof(int));

            tbl.Columns.Add("Date", typeof(DateTime));

            tbl.Columns.Add("Image", typeof(Image));

            for (int i = 0; i < RowCount; i++)

                tbl.Rows.Add(new object[] { String.Format("Name{0}", i), i, 3 - i, DateTime.Now.AddDays(i), images[i % images.Length] });

            return tbl;

        }



        public Form1()

        {

            InitializeComponent();

            gridControl1.DataSource = CreateTable(20);

            gridView1.Columns["Image"].ColumnEdit = new RepositoryItemPictureEdit();

        }

    }

}

你可能感兴趣的:(DevExpress)