C# - How To Update DataGridView Image With PictureBox Using C#

Update DataGridView Image With PictureBox In C#

C# - How To Update DataGridView Row Image Using PictureBox In C#

__________________________________________________________________________

In This C# Tutorial We Will See How To Update DataGridView Cell Images Using PictureBox In CSharp Programming Language .


Project Source Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class CSharp_Update_Datagridview_Row_Image_Using_PictureBox : Form
    {
        public CSharp_Update_Datagridview_Row_Image_Using_PictureBox()
        {
            InitializeComponent();
        }

        private void CSharp_Update_Datagridview_Row_Image_Using_PictureBox_Load(object sender, EventArgs e)
        {
            // create a DataGridView Image Column
            DataGridViewImageColumn dgvImageColumn = new DataGridViewImageColumn();
            // set a header text to DataGridView Image Column
            dgvImageColumn.HeaderText = "Image";
            dgvImageColumn.ImageLayout = DataGridViewImageCellLayout.Stretch;

            dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            dataGridView1.RowTemplate.Height = 130;

            dataGridView1.AllowUserToAddRows = false;
            // add the column to datagridview
            dataGridView1.Columns.Add(dgvImageColumn);

            // populate the datagridview with some message
            Image img1;
            img1 = Image.FromFile(@"D:\Images\img1.jpg");
            dataGridView1.Rows.Add(img1);

            Image img2;
            img2 = Image.FromFile(@"D:\Images\img2.jpg");
            dataGridView1.Rows.Add(img2);

            Image img3;
            img3 = Image.FromFile(@"D:\Images\img3.jpg");
            dataGridView1.Rows.Add(img3);

            Image img4;
            img4 = Image.FromFile(@"D:\Images\img4.jpg");
            dataGridView1.Rows.Add(img4);

            Image img5;
            img5 = Image.FromFile(@"D:\Images\img5.jpg");
            dataGridView1.Rows.Add(img5);
        }

        // Browse Image Into PictureBox
        private void BtnBrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog opf = new OpenFileDialog();
            opf.Filter = "Choose Image(*.jpg;*.png;*.gif)|*.jpg;*.png;*.gif";
            if(opf.ShowDialog() == DialogResult.OK)
            {
                pictureBox1.Image = Image.FromFile(opf.FileName);
            }
        }

        // display image into picturebox in datagridview cell click
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            Bitmap img = (Bitmap)dataGridView1.CurrentRow.Cells[0].Value;
            img.Save(ms, ImageFormat.Jpeg);
            pictureBox1.Image = Image.FromStream(ms);
        }

        // button update datagridview image
        private void Btn_Update_Image_Click(object sender, EventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            try
            {
                pictureBox1.Image.Save(ms, pictureBox1.Image.RawFormat);
                byte[] img = ms.ToArray();
                dataGridView1.CurrentRow.Cells[0].Value = img;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}


///////////////OUTPUT:
Update DataGridView Row Cell Image With PictureBox In C#
 Update DataGridView Selected Row Cell Image With PictureBox In C#



Share this

Related Posts

Previous
Next Post »