如何实现在Winform的DataGridView中展示图片?
- 内容介绍
- 文章标签
- 相关推荐
本文共计258个文字,预计阅读时间需要2分钟。
首先,添加图片序列,并绑定数据的时间会触发CellFormatting事件。在事件中,获取图片路径,读取图片并赋值给当前单元格。
csharpprivate void dataGridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e){ // 确保当前单元格是图片类型 if (e.Value is string && e.ColumnIndex==imageColumnIndex) { string imagePath=e.Value.ToString(); e.Value=LoadImage(imagePath); }}
private Image LoadImage(string imagePath){ Image image=null; try { image=Image.FromFile(imagePath); } catch { // 处理图片加载失败的情况 } return image;}
首先,要添加图片列,绑定数据的时候会触发CellFormatting事件,在事件中取出图片路径,读取图片赋值给当前单元格。
本文共计258个文字,预计阅读时间需要2分钟。
首先,添加图片序列,并绑定数据的时间会触发CellFormatting事件。在事件中,获取图片路径,读取图片并赋值给当前单元格。
csharpprivate void dataGridview1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e){ // 确保当前单元格是图片类型 if (e.Value is string && e.ColumnIndex==imageColumnIndex) { string imagePath=e.Value.ToString(); e.Value=LoadImage(imagePath); }}
private Image LoadImage(string imagePath){ Image image=null; try { image=Image.FromFile(imagePath); } catch { // 处理图片加载失败的情况 } return image;}
首先,要添加图片列,绑定数据的时候会触发CellFormatting事件,在事件中取出图片路径,读取图片赋值给当前单元格。

