如何将图像添加到DataGridView中的单个特定单元格

本文关键字:单个特 单元格 DataGridView 图像 添加 | 更新日期: 2023-09-27 18:01:16

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

例如:

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"

如何将图像添加到DataGridView中的单个特定单元格

有不止一种方法可以做到这一点,但这里有一个简单的示例,将设置一个单元格来显示图像:

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

当然,任何其他图像源也可以工作…

尽量不要泄漏位图,如果你改变他们…