C# 如何在datagridview中显示图像?C#
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16784440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How display images in datagridview? c#
提问by Reboot
I am developing an application in C # for desktop using Visual Studio Express 2010.
我正在使用 Visual Studio Express 2010 用 C# 为桌面开发一个应用程序。
I have a table in MySQL called Products with 3 fields:
我在 MySQL 中有一个名为 Products 的表,其中包含 3 个字段:
ID -> Product_Name -> product_image
ID -> Product_Name -> product_image
The field product_Imagestores the image path in my hard drive (not the image itself)
该字段product_Image将图像路径存储在我的硬盘中(不是图像本身)
An example of a record would be:
记录的一个例子是:
0001 --- Mousepad XYZ ---- c:\images\mousepad.jpg
0001 --- 鼠标垫 XYZ ---- c:\images\mousepad.jpg
I wonder how fill a datagridviewthat shows the ID, Produt name, and - especially - the product imagefor each record in my SQL query.
我想知道如何填充datagridview显示ID, Produt name, and - especially - the product image我的 SQL 查询中每条记录的 。
All the examples I found were used manual data inserts, but I am looking for an example to fill the datagridview with data from a SQL query, not a manual insertion.
我发现的所有示例都使用了手动数据插入,但我正在寻找一个示例来使用 SQL 查询中的数据填充 datagridview,而不是手动插入。
Edit:
编辑:
Thank you for help, but could not directly apply the solutions.
感谢您的帮助,但无法直接应用解决方案。
I already have a datagridview on my form, I have no need to create in runtime.
我的表单上已经有一个 datagridview,我不需要在运行时创建。
I need something like that (I'll write a generic way)
我需要这样的东西(我会写一个通用的方式)
returnMySQL = "select * from products";
while (returnMySQL)
{
??? fill datagrid with ID, product name, product image
}
回答by Freelancer
Use following Code:
使用以下代码:
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;
Referance LINK.
参考链接。
回答by Shaharyar
You can add images with the following way:
您可以通过以下方式添加图像:
//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));
回答by Gayan Chinthaka Dharmarathna
You can Doing this simple way
你可以做这个简单的方式
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;
thats All you can change Datagrid view height and with according your requirment
这就是您可以根据您的要求更改 Datagrid 视图高度的全部内容

