简体   繁体   English

如何在C#中的DataGridView中显示图片

[英]How to display picture in DataGridView in C#

I have a book database with some records. 我有一个包含一些记录的图书数据库。 Each record have one book details with book name, book link, and book picture path. 每条记录都有一个书本详细信息,包括书名,书链接和书图片路径。

I need to display records with book picture in Data Grid View control. 我需要在Data Grid View控件中显示带有书本图片的记录。 I found example how to display picture in Data Grid View but can't find example than to display picture with others fields in each record. 我找到了如何在数据网格视图中显示图片的示例,但是除了在每个记录中与其他字段一起显示图片之外,找不到其他示例。

I researched much but I can't find example like this. 我做了很多研究,但找不到类似的例子。

You can use DataGridViewImageColumn to display image. 您可以使用DataGridViewImageColumn来显示图像。 Refer the below example: 请参考以下示例:

dataGridView1.ColumnCount = 3;
dataGridView1.Columns[0].Name = "Product ID";
dataGridView1.Columns[1].Name = "Product Name";
dataGridView1.Columns[2].Name = "Product Price";

string[] row = new string[] { "1", "Product 1", "1000" };
dataGridView1.Rows.Add(row);
row = new string[] { "2", "Product 2", "2000" };
dataGridView1.Rows.Add(row);
row = new string[] { "3", "Product 3", "3000" };
dataGridView1.Rows.Add(row);
row = new string[] { "4", "Product 4", "4000" };
dataGridView1.Rows.Add(row);

DataGridViewImageColumn img = new DataGridViewImageColumn();
Image image = Image.FromFile("Image Path");
img.Image = image;
dataGridView1.Columns.Add(img);
img.HeaderText = "Image";
img.Name = "img";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM