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

发布于 2024-11-05 10:45:56 字数 1725 浏览 6 评论 0原文

我有一个网格视图,我想在其中显示路径存储在数据库中的图像。我已经尝试过这段代码,但它在所有行中只重复一张图片。谁能帮我解决这个问题?我不想使用 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();

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2024-11-12 10:45:56

您需要将 ColumnEdit 设为 RepositoryItemPictureEdit

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

columnName.ColumnEdit = rePictureEdit;

然后

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

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}");
稀香 2024-11-12 10:45:56

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

You have to use DevExpress.XtraEditors.Repository.RepositoryItemPictureEdit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文