In this article I will explain with an example, how to upload, insert and display Binary Images from database in DataGridView in Windows Forms (WinForms) Application using C# and VB.Net.
Image files will be uploaded using OpenFileDialog and then will be saved (inserted) to database table in Binary format. The saved (inserted) Image files will be retrieved and displayed in DataGridView in Windows Application using C# and VB.Net.
 
 
Database
This article makes use of a table named tblFiles whose schema is defined as follows.
Upload, Insert and Display Binary Images from database in DataGridView in Windows Application using C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Form Design
The below Form consists of a Button and a DataGridView control.
Upload, Insert and Display Binary Images from database in DataGridView in Windows Application using C# and VB.Net
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
 
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
 
 
Displaying Binary Images from database in DataGridView
Inside the Form Load event handler, columns are added to the DataGridView control.
The AutoGenerateColumns property of the DataGridView is set to False and three columns are added to the DataGridView.
The first two columns are normal Text columns while the third column is a DataGridViewImage column which is specially meant for displaying Images.
Finally the records from the Database Table are fetched and are used to populate the DataGridView control.
C#
private void Form1_Load(object sender, EventArgs e)
{
    //Set AutoGenerateColumns False.
    dataGridView1.AutoGenerateColumns = false;
 
    //Set Columns Count.
    dataGridView1.ColumnCount = 2;
 
    //Add Columns.
    dataGridView1.Columns[0].Name = "Id";
    dataGridView1.Columns[0].HeaderText = "Image Id";
    dataGridView1.Columns[0].DataPropertyName = "Id";
 
    dataGridView1.Columns[1].HeaderText = "Name";
    dataGridView1.Columns[1].Name = "Name";
    dataGridView1.Columns[1].DataPropertyName = "Name";
 
    //Add a Image Column to the DataGridView at the third position.
    DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
    imageColumn.Name = "Data";
    imageColumn.DataPropertyName = "Data";
    imageColumn.HeaderText = "Image";
    imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
    dataGridView1.Columns.Insert(2, imageColumn);
    dataGridView1.RowTemplate.Height = 100;
    dataGridView1.Columns[2].Width = 100;
 
    //Bind DataGridView.
    this.BindDataGridView();
}
 
private void BindDataGridView()
{
    string constr = @"Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true";
    using (SqlConnection conn = new SqlConnection(constr))
    {
        using (SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM tblFiles", conn))
        {
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
 
        }
    }
}
 
VB.Net
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    'Set AutoGenerateColumns False.
    dataGridView1.AutoGenerateColumns = False
 
    'Set Columns Count.
    dataGridView1.ColumnCount = 2
 
    'Add Columns.
    dataGridView1.Columns(0).Name = "Id"
    dataGridView1.Columns(0).HeaderText = "Image Id"
    dataGridView1.Columns(0).DataPropertyName = "Id"
 
    dataGridView1.Columns(1).HeaderText = "Name"
    dataGridView1.Columns(1).Name = "Name"
    dataGridView1.Columns(1).DataPropertyName = "Name"
 
    'Add a Image Column to the DataGridView at the third position.
    Dim imageColumn As DataGridViewImageColumn = New DataGridViewImageColumn()
    imageColumn.Name = "Data"
    imageColumn.DataPropertyName = "Data"
    imageColumn.HeaderText = "Image"
    imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch
    dataGridView1.Columns.Insert(2, imageColumn)
    dataGridView1.RowTemplate.Height = 100
    dataGridView1.Columns(2).Width = 100
 
    'Bind DataGridView.
    Me.BindDataGridView()
End Sub
 
Private Sub BindDataGridView()
    Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true"
    Using conn As SqlConnection = New SqlConnection(constr)
        Using sda As SqlDataAdapter = New SqlDataAdapter("SELECT * FROM tblFiles", conn)
            Dim dt As DataTable = New DataTable()
            sda.Fill(dt)
            dataGridView1.DataSource = dt
        End Using
    End Using
End Sub
 
 
Uploading and saving Image in Binary format in SQL Server database in Windows Application
The following event handler gets called when the Choose File Button is clicked and an Image file is selected.
The uploaded Image file is converted to an Array of Bytes using BinaryReader class and finally is inserted into the database table.
Finally the DataGridView is populated from database.
C#
private void btnChoose_Click(object sender, EventArgs e)
{
    using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            string fileName = openFileDialog1.FileName;
            byte[] bytes = File.ReadAllBytes(fileName);
            string contentType = "";
            //Set the contenttype based on File Extension
 
            switch (Path.GetExtension(fileName))
            {
                case ".jpg":
                    contentType = "image/jpeg";
                    break;
                case ".png":
                    contentType = "image/png";
                    break;
                case ".gif":
                    contentType = "image/gif";
                    break;
                case ".bmp":
                    contentType = "image/bmp";
                    break;
            }
 
            string constr = @"Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true";
            using (SqlConnection conn = new SqlConnection(constr))
            {
                string sql = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)";
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddWithValue("@Name", Path.GetFileName(fileName));
                    cmd.Parameters.AddWithValue("@ContentType", contentType);
                    cmd.Parameters.AddWithValue("@Data", bytes);
                    conn.Open();
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
            }
 
            this.BindDataGridView();
 
        }
    }
}
 
VB.Net
Private Sub btnChoose_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnChoose.Click
    Using openFileDialog1 As OpenFileDialog = New OpenFileDialog()
        If openFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim fileName As String = openFileDialog1.FileName
            Dim bytes As Byte() = File.ReadAllBytes(fileName)
            Dim contentType As String = ""
            Select Case Path.GetExtension(fileName)
                Case ".jpg"
                    contentType = "image/jpeg"
                Case ".png"
                    contentType = "image/png"
                Case ".gif"
                    contentType = "image/gif"
                Case ".bmp"
                    contentType = "image/bmp"
            End Select
 
            Dim constr As String = "Data Source=.\SQL2014;Initial Catalog=dbFiles;Integrated Security=true"
            Using conn As SqlConnection = New SqlConnection(constr)
                Dim sql As String = "INSERT INTO tblFiles VALUES(@Name, @ContentType, @Data)"
                Using cmd As SqlCommand = New SqlCommand(sql, conn)
                   cmd.Parameters.AddWithValue("@Name", Path.GetFileName(fileName))
                    cmd.Parameters.AddWithValue("@ContentType", contentType)
                    cmd.Parameters.AddWithValue("@Data", bytes)
                    conn.Open()
                    cmd.ExecuteNonQuery()
                    conn.Close()
                End Using
            End Using
 
            Me.BindDataGridView()
        End If
    End Using
End Sub
 
 
Screenshot
Upload, Insert and Display Binary Images from database in DataGridView in Windows Application using C# and VB.Net
 
 
Downloads