Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

How To Disolay Image Into Datagridview Via Ms Access

gridview grid datagridview

  • Please log in to reply
7 replies to this topic

#1 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 14 July 2012 - 12:46 AM

private void Form1_Load(object sender, EventArgs e)
{ OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\percis\Documents\Database 4.accdb");
conn.Open();
OleDbDataAdapter SDA = new OleDbDataAdapter("SELECT * from [Memo]", conn);
DataTable dt = new DataTable();
SDA.Fill(dt);
dataGridView1.DataSource = dt;
}
int i;
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
i = dataGridView1.SelectedCells[0].RowIndex;
textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
textBox5.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
}
when i click a row of the datagridview;data from datagridview can be displayed onto the the textbox as by the prgm.
Likewise, i want to insert image into a new column of the datagridview via MS Access; whenever i click the row of the datagridview the data will be displayed in the textbox as bf and also i want the image to be displayed in the picturebox too.

Cn u guide me step by step as how to save the image and retrieve the image by clicking on the row of the datagridview. hw should i write the codes?
Thanks.

  • 0

#2 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 14 July 2012 - 10:58 PM

Does your MS Access table contain the image you want to show? Are the images are keeping in the table as as link or as an embedded object?

However, this tutorial may help you to store images into access db (both as link or direct emebedding). That tutorial shows code in VB to retrieve and show the images.
  • 0

#3 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 15 July 2012 - 06:38 PM

Does your MS Access table contain the image you want to show? Are the images are keeping in the table as as link or as an embedded object?

However, this tutorial may help you to store images into access db (both as link or direct emebedding). That tutorial shows code in VB to retrieve and show the images.


I inserted (OLE object) BitMap Image into the column named "Image" of MS Access database.
the image is displayed in the datagridview but not displayed in the picturebox but error image is being displayed. why?
here are my codings.
int i;
	  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
	  {
		 i = dataGridView1.SelectedCells[0].RowIndex;
		 textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
		 textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
		 textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
		 textBox5.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
		 pictureBox1.ImageLocation = dataGridView1.Rows[i].Cells[4].Value.ToString();
}

any coding for picture box?
private void pictureBox1_Click(object sender, EventArgs e)
	  {
	  }

thanks.
  • 0

#4 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 16 July 2012 - 04:14 AM

I think you need to use Image property of PictureBox to assign an image to it. So last line of your dataGridview1_CellContentClick method should be like
pictureBox1.Image = (Image)dataGridView1.Rows[i].Cells[4].Value;

  • 0

#5 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 16 July 2012 - 07:21 PM

I think you need to use Image property of PictureBox to assign an image to it. So last line of your dataGridview1_CellContentClick method should be like

pictureBox1.Image = (Image)dataGridView1.Rows[i].Cells[4].Value;

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.IO;

namespace DatabaseConnection2
{
   public partial class frmDtabaseConnection : Form
   {
	  public frmDtabaseConnection()
	  {
		 InitializeComponent();
	  }
	
	  private void btn_search_Click(object sender, EventArgs e)
	  {
		 OleDbConnection conn = new OleDbConnection (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\FYP_ Prgms\DatabaseConnection2\DatabaseConnection2\Database 4.accdb");
		 DataTable dt = new DataTable();
		 OleDbDataAdapter SDA = new OleDbDataAdapter("SELECT * FROM [Memo] where Hours < " + int.Parse(txt_hours.Text) + " AND ID > " + int.Parse(txt_Id.Text) , conn);
		 SDA.Fill(dt);
		 dataGridView1.DataSource = dt;
	  }
	  private void btn_display_Click(object sender, EventArgs e)
	  {
		 System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\FYP_ Prgms\DatabaseConnection2\DatabaseConnection2\Database 4.accdb");
		 System.Data.DataTable dt = new System.Data.DataTable();
		 System.Data.OleDb.OleDbDataAdapter SDA = new System.Data.OleDb.OleDbDataAdapter("SELECT * from [Memo]", conn);
		 SDA.Fill(dt);
		 dataGridView1.DataSource = dt;
	   
	  }
	  private void btn_address_Click(object sender, EventArgs e)
	  {
		 OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\FYP_ Prgms\DatabaseConnection2\DatabaseConnection2\Database 4.accdb");
		 DataTable dt = new DataTable();
		 OleDbDataAdapter SDA = new OleDbDataAdapter("SELECT Model from [Memo]", conn);
		 SDA.Fill(dt);
		 dataGridView1.DataSource = dt;
	  }
	 
	  private void Form1_Load(object sender, EventArgs e)
	  {
		  this.BackColor = System.Drawing.Color.LightBlue;// change the form's background colour
		  OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\FYP_ Prgms\DatabaseConnection2\DatabaseConnection2\Database 4.accdb");
		  conn.Open();
		  OleDbDataAdapter SDA = new OleDbDataAdapter("SELECT * from [Memo]", conn);
		  //OleDbCommandBuilder builder = new OleDbCommandBuilder(SDA);
		  DataTable dt = new DataTable();
		  SDA.Fill(dt);
		  dataGridView1.DataSource = dt;
	  }
	  int i;
	  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
	  {
		 i = dataGridView1.SelectedCells[0].RowIndex;
		 textBox1.Text = dataGridView1.Rows[i].Cells[0].Value.ToString();
		 textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
		 textBox3.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
		 textBox5.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
		 pictureBox1.Image = (Image)dataGridView1.Rows[i].Cells[4].Value; ///>>>> error here
	  
	  }
	  private void btn_Clear_Click(object sender, EventArgs e)
	  {
		 this.BackColor = System.Drawing.Color.LightGreen;
		    textBox1.Text = "";
		    textBox2.Text = "";
		    textBox3.Text = "";
		    textBox5.Text = "";
	  }
	  private void btn_Save_Click(object sender, EventArgs e)
	  {
		
		 OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=K:\FYP_ Prgms\DatabaseConnection2\DatabaseConnection2\Database 4.accdb");
		 conn.Open();
		 string vsql = String.Format(" insert into [Memo] values ({0},'{1}','{2}','{3}')", int.Parse(textBox1.Text), textBox2.Text,textBox3.Text,textBox5.Text);
		 //textBox1 for Hours , textBox2 for Model ,textbox3 for Address
		 OleDbCommand com = new OleDbCommand(vsql, conn);
		 com.ExecuteNonQuery();
		 MessageBox.Show("Data stored successfully");
		 com.Dispose();
	  }
	  private void btn_Exit_Click(object sender, EventArgs e)
	  {
		 this.Close();
	  }
	  private void textBox4_TextChanged(object sender, EventArgs e)
	  {
	  }
	  private void btn_Delete_Click(object sender, EventArgs e)
	  {
	   
	  }
	  private void pictureBox1_Click(object sender, EventArgs e)
	  {
	  }
   }

Im still unable to display the image on the picturebox. any wrong in the namespace used?
for this
pictureBox1.Image = (Image)dataGridView1.Rows[i].Cells[4].Value;
1) error msg as InvalidCastException was unhandled. Unable to cast objec of type 'System.Byte[]' to type 'System.Drawing.Image'. pls help me resolve my pblm?

2)I inserted (OLE object) BitMap Image into the column named "Image" of MS Access database. cn i insert "jpg" type image "Package" is shown.

pls help me resolve the 2 pblms.Thanks!
  • 0

#6 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 16 July 2012 - 10:51 PM

Hmm. So it seems the image from MS Access just returned as raw bytes. So what we can do is we can create a stream (MemoryStream) from that byte array and can use Bitmap.FromStream method to create a bitmap. So try the following code

System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])dataGridView1.Rows[i].Cells[4].Value);
pictureBox1.Image = Bitmap.FromStream(stream);

Try that and let us know you you get.
  • 0

#7 percis

percis

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 72 posts
  • Programming Language:C
  • Learning:C#, (Visual) Basic

Posted 16 July 2012 - 11:18 PM

Hmm. So it seems the image from MS Access just returned as raw bytes. So what we can do is we can create a stream (MemoryStream) from that byte array and can use Bitmap.FromStream method to create a bitmap. So try the following code


System.IO.MemoryStream stream = new System.IO.MemoryStream((byte[])dataGridView1.Rows[i].Cells[4].Value);
pictureBox1.Image = Bitmap.FromStream(stream);

Try that and let us know you you get.


still there's error msg as ArgumentException was unhandled.

still there's error msg as ArgumentException was unhandled.


another question ,hw do I change .jpg image to bitmap image?
  • 0

#8 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 16 July 2012 - 11:48 PM

Can you post your whole code as well as database so that I can debug? If not, I need create a sample project to test the things.

Do you want to convert jpg to bmp using any tools or programatically? If using tools, there are online converters out there. I think you can even convert using MSPaint.
  • 1





Also tagged with one or more of these keywords: gridview, grid, datagridview

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download