如何将图像从datagridview列中获取图像到Windowsform中的图片框 [英] how to get image from datagridview column to picturebox in windowsform

查看:57
本文介绍了如何将图像从datagridview列中获取图像到Windowsform中的图片框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从datagridview列[照片]中的图像获取到Windowsform中的图片框

how to get image from datagridview column[Photos] to picturebox in windowsform

推荐答案

我尝试实现一个我想问的问题的示例.这是一个带有一个DataGridView和一个PictureBox的Windows窗体.这里没有数据绑定. dataGridView1控件具有三列,分别用于名称",一个单元按钮和一个图像.我认为您正在寻找的是添加事件以从数据网格中挑选图像的方法.我选择了CellContentDoubleClick事件,您可以在设计"视图中找到该事件,方法是单击数据网格,然后单击控件的属性窗格顶部的闪电符号.如果双击然后在CellContentDoubleClick事件上,VS将为您添加一个处理程序.这是我编写的代码:
I tried to implement an example of what I think you are asking about. It''s a windows form with one DataGridView and one PictureBox. There is no data binding here. The dataGridView1 control has three columns for Name, a cellbutton, and an image. I think what you are seeking is a way to add an event to pick the image out of the data grid. I chose the CellContentDoubleClick event that you can find in the Design view by clicking the data grid and clicking on the lightening bolt symbol at the top of the property pane for the control. If you double click then on the CellContentDoubleClick event, VS will stub a handler for you. Here is what I coded:
namespace PicturePickerForm
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();

            dataGridView1.Rows[0].Cells[1].Value = new DataGridViewButtonCell();
        }

        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            dataGridView1.Rows[dataGridView1.RowCount -1].Cells[1].Value = new DataGridViewButtonCell();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCellAddress.X != 1)
                return;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap pic = new Bitmap(openFileDialog1.FileName);
                dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value = pic;
            }
        }

        private void dataGridView1_CellContentDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView1.CurrentCellAddress.X == 2)
                pictureBox1.Image = (Image)dataGridView1.Rows[dataGridView1.CurrentCellAddress.Y].Cells[2].Value;
        }
    }
}


这篇关于如何将图像从datagridview列中获取图像到Windowsform中的图片框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆