如何将图像添加到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..