【问题标题】:display image on click from datagridview从datagridview单击时显示图像
【发布时间】: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。

标签: c# mysql


【解决方案1】:

如果我正确理解您的问题,您需要使用PictureBox.Refresh()。设置图片后使用刷新方式,例如:

pictureBox1.Image = Image.FromStream(ms);
pictureBox1.Refresh();

【讨论】:

  • 感谢您的建议,但还是一样的@Art
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-08
  • 2012-04-28
  • 1970-01-01
相关资源
最近更新 更多