如何在datagridview中显示sqlite数据库url的图像

如何在datagridview中显示sqlite数据库url的图像

问题描述:

我正在尝试从url显示他们的路径在DataGridView单元格上的sqlite数据库中的图像。

但是我只是想要显示的单元格上的字符串System.Drawing.Bitmap图片。

这是源代码。

请帮帮我。

提前谢谢



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

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();
       }
   }





我的尝试:



System.Net.WebClient webSource = new System.Net.WebClient();

string url = ds.Tables [Emp]。行[r ] .ItemArray [3] .ToString();

byte [] data = webSource.DownloadData(url);

System.IO.MemoryStream pipe = new System.IO .MemoryStream(数据);

图像jpgImage = Image.FromStream(管道);



DataGridViewImageColumn img_col = new DataGridViewImageColumn();

dataGridView1.Rows [r] .Cells [img_col]。值= jpgImage;





它没有工作



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

你需要确保它是一个ImageColumn,例如像这样

You need to make sure that it is an ImageColumn e.g. like this
//dataGridView1.Columns.Add("thumnail_url", "thumnail_url");
var dvi = new DataGridViewImageColumn {HeaderText = "thumbnail_url"};
dataGridView1.Columns.Add(dvi);