本文介绍了如何在C#中将数据从datagridview检索到picturebox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要你的帮助..
我想从datagridview检索图片到picturebox。下面是我的代码,我在datagridview单元格点击事件中收到错误:(你们可以帮助我吗?我非常需要你的帮助:(
我是什么尝试过:
这是我将图片浏览到图片框的代码:
I need your help guys..
I want to retrieve the picture from datagridview to picturebox. Below is my code and I am getting error in datagridview cell click event :( Can you guys help me? I badly need your help :(
What I have tried:
Here`s my code for browsing picture into picturebox:
string img_file = null;
private void button13_Click(object sender, EventArgs e)
{
OpenFileDialog o = new OpenFileDialog();
if (o.ShowDialog() != DialogResult.Cancel)
{
img_file = o.FileName;
pictureBox1.Image = Image.FromFile(o.FileName);
}
}
While here`s my code for saving image to database:
if (img_file != null)
{
FileStream fs = new FileStream(img_file, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] image = new byte[fs.Length];
fs.Read(image, 0, Convert.ToInt32(fs.Length));
fs.Close();
if (textBox2.Text != "" && comboBox13.Text != "" && comboBox1.Text != "" && dateTimePicker1.Text != "" && comboBox3.Text != "" && textBox4.Text != "" && textBox5.Text != "" && textBox6.Text != "" && textBox7.Text != "")
{
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("insert into tbl_Inquiry(Dog_Name,Breed,Color,Birthday,Gender,Size,Owners_Last_Name,Owners_First_Name,Contact_No,Address,Date_Registered,Image) values (@dogname,@breed,@color,@birthday,@gender,@size,@lastname,@firstname,@contactno,@address,@date,@image)", con);
con.Open();
cmd.Parameters.AddWithValue("@dogname", textBox2.Text);
cmd.Parameters.AddWithValue("@breed", comboBox13.Text);
cmd.Parameters.AddWithValue("@color", comboBox1.Text);
cmd.Parameters.AddWithValue("@birthday", dateTimePicker1.Text);
cmd.Parameters.AddWithValue("@gender", comboBox2.Text);
cmd.Parameters.AddWithValue("@size", comboBox3.Text);
cmd.Parameters.AddWithValue("@lastname", textBox5.Text);
cmd.Parameters.AddWithValue("@firstname", textBox6.Text);
cmd.Parameters.AddWithValue("@contactno", textBox7.Text);
cmd.Parameters.AddWithValue("@address", textBox4.Text);
cmd.Parameters.AddWithValue("@date", DateTime.Now.ToShortDateString());
SqlParameter prm = new SqlParameter("@image", SqlDbType.Image, image.Length, ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, image);
cmd.Parameters.Add(prm);
cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Sucessfully Save!");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Missing details! Please try again!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
And lastly, here`s my code for retrieving image from datagridview to picturebox:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
comboBox13.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
comboBox1.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
dateTimePicker1.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
comboBox2.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
comboBox3.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
textBox5.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
textBox6.Text = dataGridView1.SelectedRows[0].Cells[8].Value.ToString();
textBox7.Text = dataGridView1.SelectedRows[0].Cells[9].Value.ToString();
textBox4.Text = dataGridView1.SelectedRows[0].Cells[10].Value.ToString();
byte[] bytes = (byte[]) dtaGridView1.SelectedRows[0].Cells[11].Value;
MemoryStream ms = new MemoryStream(bytes);
pictureBox1.Image = Image.FromStream(ms);
}
推荐答案
byte[] bytes = (byte[]) dtaGridView1.SelectedRows[0].Cells[11].Value;
MemoryStream ms = new MemoryStream(bytes);
System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
pictureBox1.Image = img;
这篇关于如何在C#中将数据从datagridview检索到picturebox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!