C# datagridview 显示图片集
时间: 2023-11-26 08:45:49 浏览: 103
你可以在DataGridView中的某一列中显示图片集。具体步骤如下:
1.在DataGridView中添加一列,设置其DataPropertyName属性为图片集所在的列名。
2.在DataGridView的CellFormatting事件中,判断当前列是否为图片集所在的列,如果是,则将单元格的值转换为Image类型,并将单元格的Value属性设置为该Image对象。
3.在DataGridView的CellPainting事件中,判断当前列是否为图片集所在的列,如果是,则将单元格的Value属性转换为Image对象,并在单元格中绘制该Image对象。
代码示例:
```
private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "ImageColumn")
{
if (e.Value != null)
{
e.Value = Image.FromFile(e.Value.ToString());
}
}
}
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
if (dataGridView1.Columns[e.ColumnIndex].DataPropertyName == "ImageColumn")
{
if (e.Value != null)
{
e.PaintBackground(e.CellBounds, true);
e.Graphics.DrawImage((Image)e.Value, e.CellBounds);
e.Handled = true;
}
}
}
```
阅读全文
相关推荐















