How to show image in gridview cell from image path without using cutomunboundcolumn event?

Gagan picture Gagan · May 7, 2011 · Viewed 12.6k times · Source

I have a grid view in which I want to show images whose paths are stored in a database. I have tried this code but it repeats only one picture in all rows. Can anyone help me to solve this problem? I don't want to use the customunboundcolumn event. My code is shown below

 SqlCommand cmd = new SqlCommand("select * from picture", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        gridControl1.DataSource = ds.Tables[0];
        DevExpress.XtraGrid.Columns.GridColumn image = new DevExpress.XtraGrid.Columns.GridColumn();
        gridView1.Columns.Add(image);
        image.Caption = "Image";
        image.Name = "img";
        image.FieldName = "img";
        image.Visible = true;
        image.VisibleIndex = 3;
        int row = ds.Tables[0].Rows.Count;
        DevExpress.XtraGrid.Views.Base.ColumnView cgv = (DevExpress.XtraGrid.Views.Base.ColumnView)gridView1;
        DevExpress.XtraEditors.Repository.RepositoryItemImageEdit rpic = new DevExpress.XtraEditors.Repository.RepositoryItemImageEdit();
        rpic.NullText = "No Image";
        rpic.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Zoom;
        DevExpress.Utils.ImageCollection images = new DevExpress.Utils.ImageCollection();
        for (int i = 0; i < row; i++)
        {
            string imgpath = ds.Tables[0].Rows[i]["fpath"].ToString();

            images.Images.Add(Image.FromFile(imgpath));

            rpic.Images = images;
            gridView1.Columns["img"].ColumnEdit=rpic;

            gridView1.SetRowCellValue(i,"Image",rpic);

            gridView1.SelectRow(i);
            MessageBox.Show(gridView1.FocusedRowHandle.ToString());
            // gridView1.SetFocusedRowCellValue("Image",gridView1.Columns["img"].ColumnEdit= rpic);
        }
        gridView1.BestFitColumns();

Answer

jack_tux picture jack_tux · Nov 4, 2016

You need to make your ColumnEdit as a RepositoryItemPictureEdit

RepositoryItemPictureEdit rePictureEdit = gridControl1.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit;
rePictureEdit.NullText = " ";
rePictureEdit.SizeMode = PictureSizeMode.Squeeze;

columnName.ColumnEdit = rePictureEdit;

Then

columnName.Value = Image.FromFile($@"{imagePath}");