如何将图像添加到DataGridView中的单个特定单元格? [英] How do you add an image to a single specific cell in a DataGridView?

查看:107
本文介绍了如何将图像添加到DataGridView中的单个特定单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#和Visual Studio,我有一个带有2列的 DataGridView 。对于每一行,第一列将显示文本。对于除特定行之外的每一行,第二列将显示文本。在第二列的一个特定单元格中,我需要显示一张图像。

Using C# and Visual Studio, I have a DataGridView with 2 columns. For every row, the first column will display text. For every row EXCEPT one specific row, the second column will display text. In one specific cell in the second column, I need to show an image.

例如:

Row[0].Cell[0] = "test"  Row [0].Cell[1] = "test"
Row[1].Cell[0] = "test"  Row [1].Cell[1] = "test"
Row[2].Cell[0] = "test"  Row [2].Cell[1] = need to display an image here
Row[3].Cell[0] = "test"  Row [3].Cell[1] = "test"


推荐答案

有多种方法可以执行此操作,但以下是一个简单示例,该示例将设置一个单元格以显示图像:

There is more than one way to do it but here is a simple example that will set one single cell to show an image:

    Bitmap bmp = (Bitmap) Bitmap.FromFile(someimagefile);

    DataGridViewImageCell iCell = new DataGridViewImageCell();
    iCell.Value = bmp;
    dataGridView1[1, 2] = iCell;

当然,任何其他图像源也可以使用。.

Of course any other image source will work as well..

如果更改位图,请不要泄漏。.

Try not to leak the bitmaps if you change them..

这篇关于如何将图像添加到DataGridView中的单个特定单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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