I m working on an application where 5 images saved on a single id in the database. Now I want to retrieve these images in a datagrigview whenever they are called. My following code is working good with picturebox but I want multiple images containing the same pincode in datagridview from SQl server database.
try
{
string sql = "Select IMAGE from UserInput where PINCODE = '" + txt_LPin.Text + "'";
if (conn.State != ConnectionState.Open)
conn.Open();
cmd = new SqlCommand(sql, conn);
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
//txt_LName.Text = reader[0].ToString();
byte[] img = (byte[])(reader[1]);
if (img == null)
pb_get1.Image = null;
else
{
MemoryStream ms = new MemoryStream(img);
pb_get1.Image = Image.FromStream(ms);
}
}
else
{
txt_LPin.Text = "";
txt_LName.Text = "";
pb_get1.Image = null;
MessageBox.Show("This ID does not exist.");
}
conn.Close();
}
catch (Exception ex)
{
conn.Close();
MessageBox.Show(ex.Message);
}
I have tried following code for dataGridView but it shows cross mark intead of image on place of image.
SqlDataAdapter adpat = new SqlDataAdapter();
adpat.SelectCommand = new SqlCommand("select IMAGE from UserInput", conn);
DataTable table = new DataTable("UserInput");
adpat.Fill(table);
//create image column:
DataGridViewImageColumn photoColumn = new DataGridViewImageColumn();
photoColumn.DataPropertyName = "Picture";
photoColumn.Width = 200;
photoColumn.HeaderText = "Picture column";
photoColumn.ReadOnly = true;
photoColumn.ImageLayout = DataGridViewImageCellLayout.Normal;
dataGridView1.Columns.Add(photoColumn);
//bind data to dgv:
dataGridView1.DataSource = new BindingSource(table, null);