VB.NET - bind image in vb.net datagridview - Asked By kiruba .e on 19-Jun-12 03:12 AM

Hi,

I want to bind image from access database into datagridview vb.net windows application, just like binding of text.


So, how to do this?
pls help me

thanks
kiruba.e
Jitendra Faye replied to kiruba .e on 19-Jun-12 03:25 AM
reference from-

http://vb.net-informations.com/datagridview/vb.net_datagridview_image.htm


Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        DataGridView1.ColumnCount = 3
        DataGridView1.Columns(0).Name = "Product ID"
        DataGridView1.Columns(1).Name = "Product Name"
        DataGridView1.Columns(2).Name = "Product_Price"

        Dim row As String() = New String() {"1", "Product 1", "1000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"2", "Product 2", "2000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"3", "Product 3", "3000"}
        DataGridView1.Rows.Add(row)
        row = New String() {"4", "Product 4", "4000"}
        DataGridView1.Rows.Add(row)

        Dim img As New DataGridViewImageColumn()
        Dim inImg As Image = Image.FromFile("Image Path")
        img.Image = inImg
        DataGridView1.Columns.Add(img)
        img.HeaderText = "Image"
        img.Name = "img"

    End Sub
End Class
Lalit M replied to kiruba .e on 19-Jun-12 03:27 AM
Use this code
--------
Get The image Path from the database and then do the following :
Dim image As Image =  Image.FromFile("image path")
Dim ImageColumn As DataGridViewImageColumn =  New DataGridViewImageColumn()
ImageColumn.Image = image
ImageColumn.Name = "Name"
ImageColumn.HeaderText = "Nice Name"
dataGridView1.Columns.Insert(0, ImageColumn)

dipa ahuja replied to kiruba .e on 19-Jun-12 03:34 AM
private void button1_Click(object sender, EventArgs e)
{
  SqlConnection conn = new SqlConnection(Program.c);
 
  SqlDataAdapter da = new SqlDataAdapter("Select images from pictures", conn);
 
  DataTable dt = new DataTable();
  da.Fill(dt);
 
  conn.Open();
  //display 1st image
  if (dt.Rows.Count > 0)
  {
    byte[] storedImage = (byte[])dt.Rows[3]["images"];
    Image newImage;
 
    MemoryStream stream = new MemoryStream(storedImage);
    newImage = Image.FromStream(stream);
 
    pictureBox1.Image = newImage;
 
    dataGridView1.Rows[0].Cells[0].Value = newImage;   
  }
}