如何在datagridview中显示图像? C# [英] How display images in datagridview? c#

查看:203
本文介绍了如何在datagridview中显示图像? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio Express 2010在C#中为桌面开发应用程序。

I am developing an application in C # for desktop using Visual Studio Express 2010.

我在MySQL中有一个名为Products的表,其中包含3个字段:

I have a table in MySQL called Products with 3 fields:


ID-> Product_Name-> product_image

ID -> Product_Name -> product_image

该字段 product_Image 将映像路径存储在我的硬盘驱动器中(而不是映像本身)

The field product_Image stores the image path in my hard drive (not the image itself)

记录的一个示例是:


0001 ---鼠标垫XYZ ---- c:\images\mousepad.jpg

0001 --- Mousepad XYZ ---- c:\images\mousepad.jpg

我想知道如何填充 datagridview 来显示 ID,产品名称和-尤其是-SQL查询中每条记录的产品图片

I wonder how fill a datagridview that shows the ID, Produt name, and - especially - the product image for each record in my SQL query.

我发现的所有示例都使用了手动数据插入,但是我寻找一个示例,用SQL查询(而不是手动插入)中的数据填充datagridview。

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.

编辑:

感谢您的帮助,但无法直接应用解决方案。

Thank you for help, but could not directly apply the solutions.

我已经在窗体上有了一个datagridview,不需要在运行时创建。

I already have a datagridview on my form, I have no need to create in runtime.

我需要类似的东西(我

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;

引用链接

这篇关于如何在datagridview中显示图像? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆