Winform displays pictures in DataGridView

  • 2021-09-24 23:30:58
  • OfStack

First of all, to add a picture column, the CellFormatting event will be triggered when binding data, and the picture path will be taken out in the event, and the picture will be read and assigned to the current cell.


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;

} 

Related articles: