加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Winform在DataGridView中显示图片

发布时间:2020-12-15 06:38:21 所属栏目:百科 来源:网络整理
导读:首先,要添加图片列,绑定数据的时候会触发CellFormatting事件,在事件中取出图片路径,读取图片赋值给当前单元格。 private void dataGridview1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e){ if (dataGridview1.Columns[e.Column

首先,要添加图片列,绑定数据的时候会触发CellFormatting事件,在事件中取出图片路径,读取图片赋值给当前单元格。

private void dataGridview1_CellFormatting(object sender,DataGridViewCellFormattingEventArgs e)
{
  if (dataGridview1.Columns[e.ColumnIndex].Name.Equals("Image"))
  {
    string path = e.Value.ToString();
    e.Value = GetImage(path);
  }
}
public System.Drawing.Image GetImage(string path)
{
  System.IO.FileStream fs = new System.IO.FileStream(path,System.IO.FileMode.Open);
  System.Drawing.Image result = System.Drawing.Image.FromStream(fs);

  fs.Close();

  return result;

} 

以上就是DataGridView如何显示图片一些代码片段,希望能给大家一个参考,也希望大家多多支持编程小技巧。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读