0
    private void kayitlarilistele()
    {

        try
        {
            dataGridView1.AutoGenerateColumns = false;
            baglantim.Open();
            SqlDataAdapter listele = new SqlDataAdapter ("select * from Reaksiyon_Model", baglantim);
            DataSet dshafiza = new DataSet();
            listele.Fill(dshafiza);
            dataGridView1.DataSource = dshafiza.Tables[0];
            dataGridView1.Columns["Tanim"].DataPropertyName = "Tanim";
            dataGridView1.Columns["NeZaman"].DataPropertyName= "Reaksiyon_NeZaman";
            dataGridView1.Columns["Görsel"].DataPropertyName = "Gorsel";
            dataGridView1.Columns["ReaksiyonModeli"].DataPropertyName = "Model";
            
            baglantim.Close();   
        }

        catch (Exception hatamsj)
        {
            MessageBox.Show(hatamsj.Message);
            baglantim.Close();

When I try to get image path from database i get error

invalid cast from system.string to system.drawing.image

What's wrong with my code?

ADyson
  • 57,178
  • 14
  • 51
  • 63
Mythran
  • 37
  • 5
  • You should convert, string `Gorsel` column to `Bitmap` first. Then assign to `datagridview`. – Berkay Yaylacı Aug 22 '20 at 21:01
  • 1
    Does this answer your question? [How To Change DataType of a DataColumn in a DataTable?](https://stackoverflow.com/questions/9028029/how-to-change-datatype-of-a-datacolumn-in-a-datatable) – Alexey S. Larionov Aug 22 '20 at 21:01
  • It sounds like the database only contains the path to the image, not the image itself. So you need to find that file on disk (by using the path, obviously), and load the actual file into the datagridview, not just the path name. – ADyson Aug 22 '20 at 21:02
  • Hello guys thanks for your answers. – Mythran Aug 23 '20 at 07:20
  • No problem...did any of them help? – ADyson Aug 23 '20 at 07:57
  • Actually, first i create a datatable as dt then wrote code below; however it creates another column with name "Görsel" and shows picture as i want. yet, i just want to not create new column, instead i just want to show picture in pre-created image column. as far as i understood i need to convert data type for that. i am still looking for the answer. dt.Columns.Add ("Görsel", Type.GetType("System.Byte[]")); foreach (DataRow dr in dt.Rows) { dr["Görsel"] = File.ReadAllBytes(dr["Gorsel"].ToString()); – Mythran Aug 23 '20 at 12:59
  • It sounds like you haven't tried my suggestion yet. So try what I said - did you understand it? – ADyson Aug 23 '20 at 23:11

0 Answers0