如何在不使用cutomunboundcolumn事件的情况下从图像路径在gridview单元格中显示图像?

时间:2011-05-07 07:18:30

标签: .net winforms devexpress gridcontrol

我有一个网格视图,我想在其中显示路径存储在数据库中的图像。我试过这段代码,但它只重复所有行中的一张图片。任何人都可以帮我解决这个问题吗?我不想使用customunboundcolumn事件。我的代码如下所示

 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();

2 个答案:

答案 0 :(得分:1)

您需要将ColumnEdit设为RepositoryItemPictureEdit

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

columnName.ColumnEdit = rePictureEdit;

然后

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

答案 1 :(得分:0)

你必须使用DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit。