C# 在gridview中以windows窗体显示数据库中存储的imagepath中的图像
场景:在数据库中,我们有一个varchar类型的列,其中包含要显示的图像的路径;在dotnet应用程序中,我们有一个gridview,其中包含varchar类型的列。现在在本专栏中,我们需要显示应用程序项目图像文件夹中的图像 我们正在使用下面的方法,但正在获取写入gridviewimagecolumn中的System.drawing.bitmapC# 在gridview中以windows窗体显示数据库中存储的imagepath中的图像,c#,winforms,gridview,C#,Winforms,Gridview,场景:在数据库中,我们有一个varchar类型的列,其中包含要显示的图像的路径;在dotnet应用程序中,我们有一个gridview,其中包含varchar类型的列。现在在本专栏中,我们需要显示应用程序项目图像文件夹中的图像 我们正在使用下面的方法,但正在获取写入gridviewimagecolumn中的System.drawing.bitmap foreach (DataGridViewRow row1 in dgvDisplayTiles.Rows) { string imgPath
foreach (DataGridViewRow row1 in dgvDisplayTiles.Rows)
{
string imgPath;
imgPath= (Application.StartupPath + row1.Cells[3].Value.ToString());
Image imageFile = Image.FromFile(imgPath);
if (imgPath != null)
{
//img.Image=imgPath;
row1.Cells[3].Value = imageFile;
}
else
{
MessageBox.Show("Invalid Path");
}
}
问题是你组合了错误的路径 你应使用:
imgPath= Path.Combine(Application.StartupPath, row1.Cells[3].Value.ToString());
而不是:
imgPath= (Application.StartupPath + row1.Cells[3].Value.ToString());
完整代码:
foreach (DataGridViewRow row1 in dgvDisplayTiles.Rows)
{
string imgPath;
imgPath= Path.Combine(Application.StartupPath, row1.Cells[3].Value.ToString());
Image imageFile = Image.FromFile(imgPath);
if (imgPath != null)
{
//img.Image=imgPath;
row1.Cells[3].Value = imageFile;
}
else
{
MessageBox.Show("Invalid Path");
}
}
更新#2:
您可以创建动态列,如下所示:
为了便于理解,我创建了一个Employee类,添加了一些虚拟数据,并将其与DataGridView绑定
public class Employee
{
public string id { get; set; }
public string name { get; set; }
public string imagePath { get; set; }
}
private void bindGrid()
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee() { id = "1",name = "joseph", imagePath = "abc.png" });
employees.Add(new Employee() { id = "2", name = "Mac", imagePath = "capture2.png" });
this.dataGridView1.DataSource = employees;
DataGridViewImageColumn column = new DataGridViewImageColumn();
column.Name = "imgColumn1";
column.HeaderText = "Image";
this.dataGridView1.Columns.Add(column);
this.dataGridView1.Refresh();
foreach (DataGridViewRow row1 in dataGridView1.Rows)
{
string imgPath;
imgPath = Path.Combine(Application.StartupPath, row1.Cells[2].Value.ToString());
Image imageFile = Image.FromFile(imgPath);
if (imgPath != null)
{
row1.Cells[3].Value = imageFile;
}
else
{
MessageBox.Show("Invalid Path");
}
}
}
公共类员工
{
公共字符串id{get;set;}
公共字符串名称{get;set;}
公共字符串imagePath{get;set;}
}
私有void bindGrid()
{
列出员工=新列表();
添加(newemployee(){id=“1”,name=“joseph”,imagePath=“abc.png”});
添加(newemployee(){id=“2”,name=“Mac”,imagePath=“capture2.png”});
this.dataGridView1.DataSource=员工;
DataGridViewImageColumn=新DataGridViewImageColumn();
column.Name=“imgColumn1”;
column.HeaderText=“Image”;
this.dataGridView1.Columns.Add(column);
this.dataGridView1.Refresh();
foreach(dataGridView1.Rows中的DataGridViewRow1)
{
字符串imgPath;
imgPath=Path.Combine(Application.StartupPath,row1.Cells[2].Value.ToString());
Image imageFile=Image.FromFile(imgPath);
if(imgPath!=null)
{
行1.单元格[3]。值=imageFile;
}
其他的
{
MessageBox.Show(“无效路径”);
}
}
}
您的单元格类型是什么[3]单元格?@NearchProgrammer:varchar type它在GridView中显示相同的“System.drawing.bitmap”,行1.cells[3].Value.ToString()中存储了什么?我们在此处指定图像路径。数据库中存储了所有的ImagePath。我认为您可以添加另一列Image
类型,并将该单元格的值设置为imageFile,因为当前您的row1.Cells[3]
是string(varchar)列,要显示图像,您应该有一个ImageType
列。要求是动态创建gridview并将整个数据库表的详细信息提取到该gridview中,我们需要通过读取最后一列中的路径(即单元格[3])来显示图像。我们还可以在gridview中动态定义列类型,如果是,如何将其设置为imagetype?