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

标签 c# image datagridview cell

使用 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"

最佳答案

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

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

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

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

如果更改位图,请尽量不要泄漏它们。

关于c# - 如何将图像添加到 DataGridView 中的单个特定单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38834443/

相关文章:

c# - Jquery 在客户端对 gridview 列进行排序

c# - 我如何在 C# 中制作一个计算小时、分钟和秒的倒数计时器?

css - 如何更改图像 map 区域元素样式

c# - 使用 C# 在 asp.net 中的 SQL Server 中保存和检索图像

C# DataGridView 关注最后一条记录

c# - 分布式锁不适用于RedLock.net

c# - 在 ASP.NET Core 中尽快获取当前 URL program.cs/startup.cs

java - 在单个图像中检测多个图像

c# - 禁用 datagridview 中的复制和粘贴

c# - 设置 DisplayMember 后 DataGridViewComboBoxColumn 会发生什么情况?