Click here to Skip to main content
15,915,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to display images from url that their path is in sqlite database on DataGridView cell.
But I just a string "System.Drawing.Bitmap" on the cell that want to display images.
Here is the source code.
Please help me.
Thank you in advance

C#
string strConn = @"Data Source=C:\carku\carku.db";
   public Form1()
   {
       InitializeComponent();

   }

   private void button1_Click(object sender, EventArgs e)
   {
       using (SQLiteConnection conn = new SQLiteConnection(strConn))
       {
           conn.Open();
           SQLiteCommand comm = new SQLiteCommand("select carName,mission,color,thumnail_url From cars", conn);
           using (SQLiteDataAdapter sda =new SQLiteDataAdapter(comm))
           {
               DataSet ds = new DataSet();

               sda.Fill(ds, "Emp");
               //dataGridView1.DataSource = ds.Tables["Emp"];
               dataGridView1.Columns.Add("carName", "carName");
               dataGridView1.Columns.Add("mission", "mission");
               dataGridView1.Columns.Add("color", "color");
               dataGridView1.Columns.Add("thumnail_url", "thumnail_url");


               int row = ds.Tables["Emp"].Rows.Count - 1;
               for (int r = 0; r <= row; r++)
               {
                   dataGridView1.Rows.Add();
                   dataGridView1.Rows[r].Cells[0].Value = ds.Tables["Emp"].Rows[r].ItemArray[0];
                   dataGridView1.Rows[r].Cells[1].Value = ds.Tables["Emp"].Rows[r].ItemArray[1];
                   dataGridView1.Rows[r].Cells[2].Value = ds.Tables["Emp"].Rows[r].ItemArray[2];

                   //to show image from url on column cells[3]
                   System.Net.WebClient webSource = new System.Net.WebClient();
                   string url = ds.Tables["Emp"].Rows[r].ItemArray[3].ToString() ;
                   byte[] data = webSource.DownloadData(url);
                   System.IO.MemoryStream pipe = new System.IO.MemoryStream(data);
                   Image jpgImage = Image.FromStream(pipe);
                   dataGridView1.Rows[r].Cells[3].Value = jpgImage;

               }

           }
           conn.Close();
       }
   }


What I have tried:

System.Net.WebClient webSource = new System.Net.WebClient();
string url = ds.Tables["Emp"].Rows[r].ItemArray[3].ToString() ;
byte[] data = webSource.DownloadData(url);
System.IO.MemoryStream pipe = new System.IO.MemoryStream(data);
Image jpgImage = Image.FromStream(pipe);

DataGridViewImageColumn img_col= new DataGridViewImageColumn();
dataGridView1.Rows[r].Cells["img_col"].Value = jpgImage;


it doesn't work
Posted
Updated 11-May-16 5:21am

1 solution

You need to make sure that it is an ImageColumn e.g. like this
C#
//dataGridView1.Columns.Add("thumnail_url", "thumnail_url");
var dvi = new DataGridViewImageColumn {HeaderText = "thumbnail_url"};
dataGridView1.Columns.Add(dvi);
 
Share this answer
 
v2
Comments
hapiten 11-May-16 13:16pm    
Thank you very much

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900