winfrom datagridview ,picturebox,显示图片,以及删除问题

 private void write_listview(DataSet ds)
        {
            DataTable dt = ds.Tables[0];
            dataGridView1.DataSource = dt.DefaultView;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataGridView1.Rows[i].Cells["num"].Value = i + 1;
                dataGridView1.Rows[i].Cells["image"].Value = GetFile(AppDomain.CurrentDomain.BaseDirectory + @"File" + dt.Rows[i]["Pic"].ToString());
                
            }
        }
/// pictureBox绑定图片
               pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                
                pictureBox2.Image = GetFile(AppDomain.CurrentDomain.BaseDirectory + @"File" + path);
 ///
        /// 将文件转为内存流
        ///
        /// 
        /// 
        private MemoryStream ReadFile(string path)
        {
            if (!File.Exists(path))
                return null;
            using (FileStream file = new FileStream(path, FileMode.Open))
            {
                byte[] b = new byte[file.Length];
                file.Read(b, 0, b.Length);
 
                MemoryStream stream = new MemoryStream(b);
                return stream;
            }
        }
 
        ///
        /// 将内存流转为图片
        ///
        /// 
        /// 
        private Image GetFile(string path)
        {
            MemoryStream stream = ReadFile(path);
            return stream == null ? null : Image.FromStream(stream);
        }
or(
  public System.Drawing.Image GetImage(string path)
        {
            FileStream fs = new FileStream(path, FileMode.Open);
            Image result = Image.FromStream(fs);
 
            fs.Close();
 
            return result;
 
        }



 private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {
            //var s = dataGridView1.Columns[e.ColumnIndex].Name;
            path = dataGridView1.Rows[e.RowIndex].Cells["imagepath"].Value.ToString();
             id = dataGridView1.Rows[e.RowIndex].Cells["Column2"].Value.ToString();
            if (dataGridView1.Columns[e.ColumnIndex].Name == "image")
            {
                pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
                
                pictureBox2.Image = GetFile(AppDomain.CurrentDomain.BaseDirectory + @"File" + path);
 
            }
            if (dataGridView1.Columns[e.ColumnIndex].Name == "Column_del")
            {
                pictureBox2.Image = null;
                
                if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + @"File" + path))
                {
                    File.Delete(AppDomain.CurrentDomain.BaseDirectory + @"File" + path);
                }
                string sqlstr = "DELETE from [Image] where ID= '" + id + "'";
                int count = SQLiteHelper.ExecuteNonQuery(sqlstr);
              
                if (count > 0)
                {
                    this.dataGridView1.Rows.Remove(this.dataGridView1.Rows[e.RowIndex]);
                    MessageBox.Show("删除成功!");
                    sqlstr = "SELECT * FROM [Image] where [EID]='" + editsrt + "'";//"SELECT * FROM [Image] where EID='" + eid + "'";
                    DataSet ds_img = SQLiteHelper.ExecuteQuery(sqlstr);
                    write_listview(ds_img);
 
                }
 
            }



原文地址:https://www.cnblogs.com/ft-Pavilion/p/5642509.html