如何在datagridview中显示图像?c#
本文关键字:显示图 图像 显示 datagridview | 更新日期: 2023-09-27 18:15:43
我正在使用Visual Studio Express 2010开发一个c#桌面应用程序。
我在MySQL中有一个名为Products的表,有3个字段:
ID -> Product_Name -> product_image
字段product_Image
存储我硬盘中的映像路径(不是映像本身)
记录的一个例子是:
0001—Mousepad XYZ ---- c:'images' Mousepad .jpg
我想知道如何填充datagridview
,显示ID, Produt name, and - especially - the product image
在我的SQL查询中的每条记录。
我发现的所有示例都是使用手动数据插入,但我正在寻找一个示例来填充来自SQL查询的数据的datagridview,而不是手动插入。
编辑:感谢您的帮助,但无法直接应用解决方案。
我已经有一个datagridview在我的表单,我没有必要在运行时创建。
我需要这样的东西(我会写一个通用的方式)
returnMySQL = "select * from products";
while (returnMySQL)
{
fill datagrid with ID, product name, product image
}
使用以下代码:
Bitmap img;
img = new Bitmap(@"c:'images'mousepad.jpg");
// Create the DGV with an Image column
DataGridView dgv = new DataGridView();
this.Controls.Add(dgv);
DataGridViewImageColumn imageCol = new DataGridViewImageColumn();
dgv.Columns.Add(imageCol);
// Add a row and set its value to the image
dgv.Rows.Add();
dgv.Rows[0].Cells[0].Value = img;
参考链接
您可以通过以下方式添加图像:
//you need to perform some parsing to retrieve individual values of ID, Name and ImagePath
string path = @"c:'images'mousepad.jpg";
string ID = "0001";
string Product_Name = "Mousepad XYZ";
dataGridView1.Rows.Add(ID, Product_Name, Bitmap.FromFile(path));
你可以这么做
SqlConnection conn=New SqlConnection("SERVER=127.0.0.1;DATABASE=bdss;UID=sa;PASSWORD=1234");
SqlDataAdapter adpt = new SqlDataAdapter("select * from products",conn);
DataTable dt = new System.Data.DataTable();
adpt.Fill(dt);
int count = dt.Rows.Count;
dataGridView1.DataSource = dt;