0

I have an Access database with pictures and other data stored. I want to show the picture from the database in a DatagridView.

This works but the image height is very small in the DatagridView. I also want to stretch the image.

How do I do this ?

Here is where I bind the data from the Access database to the datagridview:

conn.Open();

OleDbCommand cmd = new OleDbCommand(cmdstr, conn);
DataTable table = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
da.Fill(table);

DatagridView1.DataSource = table;

conn.Close();

With this code I created a column to show the picture, but I can't bind the data from the database to this column.

DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
photoColumn.DataPropertyName = "Photo";
photoColumn.Width = 200;
photoColumn.HeaderText = "Image";
photoColumn.ReadOnly = true;
photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;

DatagridView1.Columns.Add(photoColumn);
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user2811133
  • 69
  • 2
  • 7

1 Answers1

0

The data of your database are bind by the fist code segment you have written, speciffically:

DatagridView1.DataSource = table;

binds the data brought by your query to your Datagridview. All you have to do is to set up the query accordingly to retrieve the image.

Franlky with Dataadapter I am not quite sure how to store an image but if you use Sqlreader: ...

if (reader.HasRows)
    {
      while (reader.Read())
     {
         string s1 = reader[0].ToString(); ///1st field you are interested
        string s2 = reader[1].ToString();  //2nd field you want
        byte[] img = (byte[])(reader[2]);   //your photo image
      }
     }

Finally be sure, to set on the designer AutogenerateColumns property to true for your Datagrid.

apomene
  • 14,282
  • 9
  • 46
  • 72