本文介绍了在DataGridViewImageColumn中渲染图像时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我正在开发一个窗口应用程序。
在中呈现图像时DataGridViewImageColumn
,只有第一张图片显示在所有行中。这是我的代码...
Hello,
I am developing a window application.
At the time of rendering images in DataGridViewImageColumn
, only first image is showing in all rows.This is my code...
private void BindGrid()
{
DataSet ds = objStaff.GetStaff(1);
if (ds != null)
{
grdDetail.DataSource = ds.Tables[0];
grdDetail.Columns["StaffId"].Visible = false;
grdDetail.Columns["Image"].Visible = false;
DataGridViewImageColumn img = new DataGridViewImageColumn();
//int number_of_rows = grdDetail.RowCount;
for (int i = 0; i < grdDetail.Rows.Count; i++)
{
img.HeaderText = "Image";
img.Width = 80;
string imagePath = ds.Tables[0].Rows[i][1].ToString();
Image image = Image.FromFile(imagePath);
img.Image = image;
//grdDetail.Rows[i].Cells["Picture"].Value = image;
grdDetail.Columns.Add(img);
}
grdDetail.Refresh();
}
}
我该怎么办?请帮忙。
What should I do? Please help.
推荐答案
private void BindGrid()
{
DataSet ds = objStaff.GetStaff(1);
if (ds != null)
{
DataTable dtable = new DataTable();
DataColumn dcol = new DataColumn("myImage");
dtable = ds.Tables[0].Copy;
dcol.AllowDBNull = true;
dcol.DataType = System.Type.GetType("System.Byte[]");
dcol.Caption = "myImage";
dtable.Columns.Add(dcol);
foreach (DataRow drow in dtable.Rows)
{
string imagePath = drow[1].ToString();
Image image = Image.FromFile(imagePath);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); // your image format here
drow["myImage"] = ms.ToArray();
}
grdDetail.DataSource = dtable;
grdDetail.Columns["StaffId"].Visible = false;
grdDetail.Columns["Image"].Visible = false;
}
}
这篇关于在DataGridViewImageColumn中渲染图像时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!