10 Replies - 6943 Views - Last Post: 10 February 2013 - 04:39 PM Rate Topic: -----

#1 AnagJohari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 10

How to display the images in Datagridview from the database

Posted 09 February 2013 - 04:58 AM

Hello All,
I have patient_table in the database. which contain Id, name ,Image & other fields
I have inserted the image in the Byte form in the database.
Now i want to display all records in the Datagrid view in windows Form.

i am able to display id & Name & all fields but unable to display the image of a patient in the datagrid view

I put all the records in the generic list:-
 public static List<Personal_Details> AllMembers()
        {
 
            List<Personal_Details> listMembers = new List<Personal_Details>();
            Personal_Details pDetails;
            try
            {
 
 
                String Constring = ConnectionString.dataBaseConnectionString;
                OleDbConnection conn = new System.Data.OleDb.OleDbConnection(Constring);
                string query = @"Select * from member_table";
 
                OleDbCommand cmd = new OleDbCommand(query, conn);
                conn.Open();
                OleDbDataReader reader = cmd.ExecuteReader();
 
                while (reader.Read())
                {
 
                    pDetails = new Personal_Details();
                    pDetails.memberId = Int64.Parse(reader["memberId"].ToString());
                    pDetails.addressStreet = reader["addressStreet"].ToString();
                    pDetails.addressArea = reader["addressArea"].ToString();
                    pDetails.addressCity = reader["addressCity"].ToString();
                    pDetails.state = reader["state"].ToString();
                    pDetails.pincode = reader["pincode"].ToString();
                    pDetails.category = reader["category"].ToString();
                    pDetails.DOB = reader["DOB"].ToString();
                    pDetails.education = reader["education"].ToString();
                    pDetails.memberFirstName = reader["memberFirstName"].ToString();
                    pDetails.memberLastName = reader["memberLastName"].ToString();
                    pDetails.memberMiddleName = reader["memberMiddleName"].ToString();
 
 
 
                    pDetails.imageByte = (Byte[])(reader["image"]);
                    // pDetails.mobileNo=Double.Parse(reader["mobile_no"].ToString());
                    //pDetails.profession=reader["profession"].ToString();
                    //pDetails.role=reader["role"].ToString();
                    pDetails.sex=reader["sex"].ToString();
                    // pDetails.subRole=reader["subrole"].ToString();
 
                    listMembers.Add(pDetails);
                }
 
                reader.Close();
                conn.Close();
 
            }
            catch (Exception ex)
            {
 
            }
            return listMembers;
 
        }
private void ListAllMembers_Load(object sender, EventArgs e)
        {
            List<Personal_Details> listAllMembers = Personal_Details.AllMembers();
 
 
            dgvAllMembers.DataSource = listAllMembers;
 
 
        }
 


i am unable to write the code for displaying the image on data gridview

I am unable to find the solution for binding the image field in the datagrid view.

Please help me as soon as possible.

Is This A Good Question/Topic? 0
  • +

Replies To: How to display the images in Datagridview from the database

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3531
  • View blog
  • Posts: 10,934
  • Joined: 05-May 12

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 05:08 AM

It's because you are loading your image as a byte array into your PersonDetails class. If you change the type of that field into an Image or Bitmap, then you'll have better luck. Given a byte array, how is the DataGridView supposed to magically know that you actually wanted to render an image, and not something else?
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,739
  • Joined: 02-June 10

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 07:32 AM

Continuing from Skydiver's good advice & observation...

Quote

pDetails.imageByte = (Byte[])(reader["image"]);



I'm going to guess by the name .imageByte that this property is a byte[]. So what do you have that property diong with the byte[] being set?

Properties have a set method built in. So you could take the incoming value (a byte array) and use that to set the Bitmap or Image value of the object.

class personal_details
{
   private bitmap Photo;
   public byte[] imageByte
   {
       set
       {
         Photo = new Bitmap(value);//Look up the correct overload on MSDN
       }
    }
}


This might help as well depending on the format of the byte[]
http://stackoverflow...y-of-pixel-data
Was This Post Helpful? 0
  • +
  • -

#4 AnagJohari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 10

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 08:32 AM

View PostSkydiver, on 09 February 2013 - 05:08 AM, said:

It's because you are loading your image as a byte array into your PersonDetails class. If you change the type of that field into an Image or Bitmap, then you'll have better luck. Given a byte array, how is the DataGridView supposed to magically know that you actually wanted to render an image, and not something else?


Hi
I did not understand your point. i know there is something which we need to convert the byte[] in to image.
i am unable to find the solution. i know how to display the single image on windows application by using picture box, but unable to find the solution to display the image on data grid view.

I need your pointer.
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,739
  • Joined: 02-June 10

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 09:16 AM

I just gave you links for how to convert a byte[] into a bitmap. Along with the code for a property to do this.

How much more of a 'pointer' do you want? We aren't going to write your code for you.

Fair warning: Your next post needs to show us the effort you have made to incorporate the information you've received. If its another "I don't understand, show me" post I'll close this as just another "gimme teh codez" post.
Was This Post Helpful? 0
  • +
  • -

#6 AnagJohari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 10

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 12:23 PM

View PosttlhIn`toq, on 09 February 2013 - 07:32 AM, said:

Continuing from Skydiver's good advice & observation...

Quote

pDetails.imageByte = (Byte[])(reader["image"]);



I'm going to guess by the name .imageByte that this property is a byte[]. So what do you have that property diong with the byte[] being set?

Properties have a set method built in. So you could take the incoming value (a byte array) and use that to set the Bitmap or Image value of the object.

class personal_details
{
   private bitmap Photo;
   public byte[] imageByte
   {
       set
       {
         Photo = new Bitmap(value);//Look up the correct overload on MSDN
       }
    }
}


This might help as well depending on the format of the byte[]
http://stackoverflow...y-of-pixel-data

Was This Post Helpful? 0
  • +
  • -

#7 AnagJohari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 10

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 12:35 PM

class personal_details
02
{
03
   private bitmap Photo;
04
   public byte[] imageByte
05
   {
06
       set
07
       {
08
         Photo = new Bitmap(value);(there is no overload function which accept byteArray)
09
       }
10
    }
11
}


Properies:
public Bitmap photo
{
set { _photo = value; }
            
}
        




I have to right the code like this
public static List<Personal_Details> AllMembers()
{
ImageConverter ic;
                Image img;
                Byte[] cbyte;
while (reader.Read())
 {
ic=new ImageConverter();
cbyte = (Byte[])(reader["image"]);
img = (Image)ic.ConvertFrom(cbyte);
pDetails.photo = new Bitmap(img);
}

}


Binding the data in DataGrid View
List<Personal_Details> listAllMembers = Personal_Details.AllMembers();
            
           
            dgvAllMembers.DataSource = listAllMembers;



Still Facing the problem?
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5464
  • View blog
  • Posts: 11,739
  • Joined: 02-June 10

Re: How to display the images in Datagridview from the database

Posted 09 February 2013 - 01:18 PM

View PostAnagJohari, on 09 February 2013 - 01:35 PM, said:

Still Facing the problem?


And to be clear the problem is:

Quote

i am able to display id & Name & all fields but unable to display the image of a patient in the datagrid view


Is that right?

Just as a diagnostic, try making a new form and put the image you've created onto its background and show it. Just to make sure you really do have an image. It might be showing fine in your datagrid, but be null or blank so fooling you into thinking its not working.
Was This Post Helpful? 0
  • +
  • -

#9 AnagJohari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 10

Re: How to display the images in Datagridview from the database

Posted 10 February 2013 - 10:34 AM

View PosttlhIn`toq, on 09 February 2013 - 09:16 AM, said:

I just gave you links for how to convert a byte[] into a bitmap. Along with the code for a property to do this.

How much more of a 'pointer' do you want? We aren't going to write your code for you.

Fair warning: Your next post needs to show us the effort you have made to incorporate the information you've received. If its another "I don't understand, show me" post I'll close this as just another "gimme teh codez" post.

Was This Post Helpful? 0
  • +
  • -

#10 AnagJohari  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 28-May 10

Re: How to display the images in Datagridview from the database

Posted 10 February 2013 - 10:45 AM

Hi,
I got a little success to display the image on datagrid view now i am facing the another problem

The image which is last at the record in the database is shown for all rows in the datagrid view.

i have attached the output of the following code. please review it which will helps you to understand my problem,

please tell me where i am wrong.




private void ListAllMembers_Load(object sender, EventArgs e)
        {
            MemoryStream img=null;
            Image photo=null;

            DataGridViewImageColumn imageColumn = new DataGridViewImageColumn();
            imageColumn.Width = 100;
            dgvAllMembers.RowTemplate.Height = 100;
            imageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;
            List<Personal_Details> listAllMembers = Personal_Details.AllMembers();
            foreach (Personal_Details pDetails in listAllMembers)
            {
                img = new MemoryStream(pDetails.imageByte);
                 if(pDetails.imageByte.Length>0)
                 {
                    photo = Image.FromStream(img);
                    imageColumn.Image = photo;
                }
                else
                {
                    
                    imageColumn.Image = global::HRManagement.Properties.Resources.unknown;
                }

            }
            dgvAllMembers.Columns.Add(imageColumn);
            dgvAllMembers.DataSource = listAllMembers;
            
            
            

        }


Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3531
  • View blog
  • Posts: 10,934
  • Joined: 05-May 12

Re: How to display the images in Datagridview from the database

Posted 10 February 2013 - 04:39 PM

Why are you assigning the photo to your imageColumn? You should be assigning the photo to your pDetail's field that exposes an image.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1