Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 206
Gagan Avatar asked Nov 14 '22 23:11

Gagan


1 Answers

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}");
like image 163
jack_tux Avatar answered Dec 22 '22 09:12

jack_tux