【发布时间】:2017-03-19 07:48:28
【问题描述】:
当我在 datagridview 上单击它时,如何显示已存储在数据库中的图像以显示在图片框上...我对 c# 编码还是很陌生,所以我真的不知道如何这样做
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
if (dataGridView1.SelectedRows.Count > 0)
{
string ItemID = dataGridView1.SelectedRows[0].Cells[0].Value + string.Empty;
string ItemName = dataGridView1.SelectedRows[0].Cells[1].Value + string.Empty;
string SupplierID = dataGridView1.SelectedRows[0].Cells[2].Value + string.Empty;
string Quantity = dataGridView1.SelectedRows[0].Cells[3].Value + string.Empty;
string Price = dataGridView1.SelectedRows[0].Cells[4].Value + string.Empty;
MemoryStream ms = new MemoryStream();
Bitmap img = (Bitmap)dataGridView1.CurrentRow.Cells[1].Value;
img.Save(ms, ImageFormat.Jpeg);
pictureBox1.Image = Image.FromStream(ms);
textBox1.Text = ItemID;
textBox2.Text = ItemName;
textBox3.Text = SupplierID;
textBox4.Text = Quantity;
textBox5.Text = Price;
}
【问题讨论】:
-
你为什么要访问已经取出ItemName的Cell[1]???另外:(正确的)单元格具有什么数据类型?它是一个图像列吗?然后你可以简单地使用它的 FormattedValue 并将其分配给 pbx.Image。