In this article, I am going to show you, How to show images in DataGridView in Windows forms c#. Its easy to bind the DataGridView with the database table which is contain binary image. You can check this code to bind the DataGridView with the image using Entity Framework.
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace BankingApplication_Tutorial
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Id";
dataGridView1.Columns[0].HeaderText = "Image Id";
dataGridView1.Columns[0].DataPropertyName = "Id";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].DataPropertyName = "Name";
DataGridViewImageColumn Imagecolumn = new DataGridViewImageColumn();
Imagecolumn.Name = "Data Image";
Imagecolumn.DataPropertyName = "Data";
Imagecolumn.HeaderText = "Image Show";
Imagecolumn.ImageLayout = DataGridViewImageCellLayout.Normal;
dataGridView1.Columns.Insert(2, Imagecolumn);
dataGridView1.RowTemplate.Height = 100;
dataGridView1.Columns[2].Width = 100;
this.bindDataGridView();
}
private void bindDataGridView()
{
// throw new NotImplementedException();
banking_dbEntities1 dbe = new banking_dbEntities1();
var items = dbe.DataGridImages.ToList();
dataGridView1.DataSource = items;
}
private void button1_Click(object sender, EventArgs e)
{
banking_dbEntities1 dbe = new banking_dbEntities1();
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
string filename = dialog.FileName;
byte[] bytes = File.ReadAllBytes(filename);
DataGridImage img = new DataGridImage();
img.Name = Path.GetFileName(filename);
img.Data = bytes;
dbe.DataGridImages.Add(img);
dbe.SaveChanges();
}
}
bindDataGridView();
}
}
}
using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace BankingApplication_Tutorial
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.ColumnCount = 2;
dataGridView1.Columns[0].Name = "Id";
dataGridView1.Columns[0].HeaderText = "Image Id";
dataGridView1.Columns[0].DataPropertyName = "Id";
dataGridView1.Columns[1].Name = "Name";
dataGridView1.Columns[1].HeaderText = "Name";
dataGridView1.Columns[1].DataPropertyName = "Name";
DataGridViewImageColumn Imagecolumn = new DataGridViewImageColumn();
Imagecolumn.Name = "Data Image";
Imagecolumn.DataPropertyName = "Data";
Imagecolumn.HeaderText = "Image Show";
Imagecolumn.ImageLayout = DataGridViewImageCellLayout.Normal;
dataGridView1.Columns.Insert(2, Imagecolumn);
dataGridView1.RowTemplate.Height = 100;
dataGridView1.Columns[2].Width = 100;
this.bindDataGridView();
}
private void bindDataGridView()
{
// throw new NotImplementedException();
banking_dbEntities1 dbe = new banking_dbEntities1();
var items = dbe.DataGridImages.ToList();
dataGridView1.DataSource = items;
}
private void button1_Click(object sender, EventArgs e)
{
banking_dbEntities1 dbe = new banking_dbEntities1();
using (OpenFileDialog dialog = new OpenFileDialog())
{
if (dialog.ShowDialog() == DialogResult.OK)
{
string filename = dialog.FileName;
byte[] bytes = File.ReadAllBytes(filename);
DataGridImage img = new DataGridImage();
img.Name = Path.GetFileName(filename);
img.Data = bytes;
dbe.DataGridImages.Add(img);
dbe.SaveChanges();
}
}
bindDataGridView();
}
}
}