简体   繁体   English

如何在datagridview中显示图像? C#

[英]How display images in datagridview? c#

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 with three fields的表:

ID -> Product_Name -> product_image ID - > Product_Name - > product_image

The field product_Image stores 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 datagridview that shows the ID, Produt name, and - especially - the product image for each record in my SQL query. 我想知道如何填充显示ID, Produt name, and - especially - the product imagedatagridview ID, Produt name, and - especially - the product image我的SQL查询中每条记录ID, Produt name, and - especially - the product image

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
}

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 . Referance LINK

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));

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视图高度

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

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