DataGridView显示图片 Winform在DataGridView中显示图片

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

DataGridView显示图片 Winform在DataGridView中显示图片

秦风   2021-03-22 我要评论
想了解Winform在DataGridView中显示图片的相关内容吗,秦风在本文为您仔细讲解DataGridView显示图片的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:DataGridView,图片,CellFormatting,事件,下面大家一起来学习吧。

首先,要添加图片列,绑定数据的时候会触发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如何显示图片一些代码片段,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们