How to Display and Resize the Image in the DataGridView Using Access Database and VB.Net

In this tutorial, I will teach you how to display and resize the image in the datagridview using access database and visual basic.net. This sample project will help you to display the image from the access database to the datagridview. It also has the capability to fit the displayed image in the datagridview according to the column width and row height. Follow the step by step guide to know how it works. Hope this project will help your current problem.

Creating an Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application in visual basic. ps1

Step 2

Do the form just like shown below. ps2

Step 3

Press F7 to open the code editor. In the code editor, add a namespace for OLeDB to access OLeDB libraries .
  1. Imports System.Data.OleDb

Step 4

Create a connection between the access database and c#. After that, declare all the classes and variables that are needed.
  1.  
  2.     Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & Application.StartupPath & "\dbsaveimg.accdb")
  3.     Dim cmd As OleDbCommand
  4.     Dim da As OleDbDataAdapter
  5.     Dim dt As DataTable
  6.     Dim sql As String

Step 5

Create a method for retrieving images in the database.
  1.    
  2.     Private Sub loadData(sql As String, dtg As DataGridView)
  3.         Try
  4.             con.Open()
  5.  
  6.             cmd = New OleDbCommand
  7.             da = New OleDbDataAdapter
  8.             dt = New DataTable
  9.  
  10.             With cmd
  11.                 .Connection = con
  12.                 .CommandText = sql
  13.             End With
  14.  
  15.             da.SelectCommand = cmd
  16.             da.Fill(dt)
  17.  
  18.             dtg.DataSource = dt
  19.             dtg.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
  20.  
  21.         Catch ex As Exception
  22.             MsgBox(ex.Message)
  23.         End Try
  24.     End Sub

Step 6

Write the following code for resizing the image in displaying in the datagridview.
  1.    
  2.     Private Sub DataGridView1_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles DataGridView1.CellPainting
  3.  
  4.         If (e.RowIndex < 0 Or e.ColumnIndex < 0) Then Return
  5.  
  6.         If DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex).ValueType = GetType(Byte()) Then
  7.             CType(DataGridView1.Rows(e.RowIndex).Cells(e.ColumnIndex),
  8.               DataGridViewImageCell).ImageLayout = DataGridViewImageCellLayout.Zoom
  9.         End If
  10.  
  11.     End Subssss

Step 7

Do the following code for displaying the image in the datagridview when the form is loaded.
  1.    
  2.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  3.         sql = "Select img as Picture From tblimage"
  4.         loadData(sql, DataGridView1)
  5.     End Subs
Download the complete source code and run it on your computer. For any questions about this article. You can contact me @ Email – [email protected] Mobile No. – 09305235027 – TNT Or feel free to comment below

Add new comment