软件编程
位置:首页>> 软件编程>> C#编程>> Winform在DataGridView中显示图片

Winform在DataGridView中显示图片

作者:秦风  发布时间:2023-06-26 21:02:48 

标签:DataGridView

首先,要添加图片列,绑定数据的时候会触发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;

}
0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com