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#



C# - How To Get The Average Value From DataGridView Column In C#

datagridview column average value

C# - How To Get The Average Value In DataGridView Column Using C#

                                                                                                                         

In This C# Tutorial  We Will See How To Get The Average Value From DataGridView  Column And Display It In TextBox Using CSharp Programming Language.


Project Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

  private void Csharp_Datagridview_Column_Cells_Average_Load(object sender, EventArgs e)
        {
            // populate datagridview with some data
            Random r = new Random();
            for (int i = 0; i <= 10; i++)
            {
                dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), r.Next(20, 65).ToString());
            }

            dataGridView1.AllowUserToAddRows = false;

            // method 1
           textBoxAvg.Text = (from DataGridViewRow row in dataGridView1.Rows
                          where row.Cells[2].FormattedValue.ToString() != string.Empty
                           select Convert.ToInt32(row.Cells[2].FormattedValue)).Average().ToString("00.000");*/

            // method 2
            int sum = 0;
            double avg = 0;
            int dataCount = dataGridView1.Rows.Count;
            for(int i = 0; i <= dataGridView1.Rows.Count-1; i++)
            {
                sum = sum + int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
            }
            avg = (double)sum/dataCount;
            textBoxAvg.Text = avg.ToString("00.000");

            // method 3
            int[] columnData = (from DataGridViewRow row in dataGridView1.Rows
                                where row.Cells[2].FormattedValue.ToString() != string.Empty
                                select Convert.ToInt32(row.Cells[2].FormattedValue)).ToArray();

            textBoxAvg.Text = columnData.Average().ToString("00.000");
        }
    }
}


      
///////////////OUTPUT:





c# datagridview average column value
c# datagridview average column value




C# - How To Get The Minimum Value From DataGridView Column Using C#

datagridview column minimum value

C# - How To Get The Min Value In DataGridView Column Using C#

                                                                                                                         

In This C# Tutorial  We Will See How To Get The Minimum Value From DataGridView  Column And Display It In TextBox Using CSharp Programming Language.


Project Source Code:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Csharp_Datagridview_Column_Cells_Min_Load(object sender, EventArgs e)
        {
            // populate datagridview with some data
            Random r = new Random();
            for (int i = 0; i <= 10; i++)
            {
                dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), r.Next(20, 65).ToString());
            }

            dataGridView1.AllowUserToAddRows = false;

            // method 1
            textBoxMin.Text = (from DataGridViewRow row in dataGridView1.Rows
                               where row.Cells[2].FormattedValue.ToString() != string.Empty
                               select Convert.ToInt32(row.Cells[2].FormattedValue)).Min().ToString();
     
           

            // method 2
            int min = 0;
            for (int i = 0; i <= dataGridView1.Rows.Count-1; i++ )
            {
                if(i == 0)
                {
                   min = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                }
                
                if(min > int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()))
                {
                    min = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                }
            }
            textBoxMin.Text = min.ToString();

            // method 3
            int[] columnData = new int[dataGridView1.Rows.Count];
            columnData = (from DataGridViewRow row in dataGridView1.Rows
                          where row.Cells[2].FormattedValue.ToString() != string.Empty
                          select Convert.ToInt32(row.Cells[2].FormattedValue)).ToArray();
            textBoxMin.Text = columnData.Min().ToString();

       }
    }
}


      
///////////////OUTPUT:

datagridview column min value
c# datagridview column minimum value








C# - How To Get The Maximum Value From DataGridView Column Using C#


datagridview column max value

C# - How To Get The Value Maximum In DataGridView Column Using C#

                                                                                                                         

In This C# Tutorial  We Will See How To Get The Maximum Value From DataGridView  Column And Display It In TextBox Using CSharp Programming Language.


Project Source Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

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

        private void Csharp_Datagridview_Column_Cells_Max_Load(object sender, EventArgs e)
        {
            // populate datagridview with some data
            Random r = new Random();
            for (int i = 0; i <= 10; i++)
            {
                dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(),r.Next(20,65).ToString());
            }

            dataGridView1.AllowUserToAddRows = false;

            // method 1
            textBoxMax.Text = (from DataGridViewRow row in dataGridView1.Rows
                                where row.Cells[2].FormattedValue.ToString() != string.Empty
                                 select Convert.ToInt32(row.Cells[2].FormattedValue)).Max().ToString();
            

            // method 2
            int max = 0;
            for (int i = 0; i <= dataGridView1.Rows.Count-1; i++)
            {
                if(max < int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString()))
                {
                    max = int.Parse(dataGridView1.Rows[i].Cells[2].Value.ToString());
                }
            }

            textBoxMax.Text = max.ToString();

            // method 3
            int[] columnData = new int[dataGridView1.Rows.Count];
            columnData = (from DataGridViewRow row in dataGridView1.Rows
                            where row.Cells[2].FormattedValue.ToString() != string.Empty
                            select Convert.ToInt32(row.Cells[2].FormattedValue)).ToArray();

            textBoxMax.Text = columnData.Max().ToString();
        }
    }
}

      
///////////////OUTPUT:





c# max datagridview column value
c# max datagridview column value




VB.NET - How To Get The DataGridView Column Max, Min, Sum, Average Value Using VB NET


vb datagridview column max min sum average value

VB.NET - How To Get The Sum, Max, Min, Avg Value Of A DataGridView Column In VB NET

                                                                                                                         

In This VB.NET Tutorial  We Will See How To Get The Average, Max, Min, Sum Value From Specific DataGridView Column And Display Them In TextBoxes Using VBNET Programming Language.


Project Source Code:

Public Class VB_Datagridview_Column_Cells_Average_Sum_Max_Min_Value

    Private Sub VB_Datagridview_Column_Cells_Average_Sum_Max_Min_Value_Load(sender As Object, e As EventArgs) Handles MyBase.Load

       ' populate datagridview with some data
        Dim rand As New Random()
        For i As Integer = 0 To 11 Step +1
            dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), rand.Next(20, 65).ToString())
        Next
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        dataGridView1.AllowUserToAddRows = False

' Method 1
      
        '  Sum Value
        TextBoxSum.Text = (From row As DataGridViewRow In dataGridView1.Rows
                           Where row.Cells(2).FormattedValue.ToString() <> String.Empty
                           Select Convert.ToInt32(row.Cells(2).FormattedValue)
                           ).Sum().ToString()

        ' Max Value
        TextBoxMax.Text = (From row As DataGridViewRow In dataGridView1.Rows
                           Where row.Cells(2).FormattedValue.ToString() <> String.Empty
                           Select Convert.ToInt32(row.Cells(2).FormattedValue)
                           ).Max().ToString()

        ' Min Value
        TextBoxMin.Text = (From row As DataGridViewRow In dataGridView1.Rows
                           Where row.Cells(2).FormattedValue.ToString() <> String.Empty
                           Select Convert.ToInt32(row.Cells(2).FormattedValue)
                           ).Min().ToString()

        ' Average Value
        textBoxAvg.Text = (From row As DataGridViewRow In dataGridView1.Rows
                           Where row.Cells(2).FormattedValue.ToString() <> String.Empty
                           Select Convert.ToInt32(row.Cells(2).FormattedValue)
                           ).Average().ToString("00.000")

        ' Method 2
        Dim sum As Integer
        Dim min As Integer
        Dim max As Integer
        Dim avg As Double

        For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step +1

            sum = sum + dataGridView1.Rows(i).Cells(2).Value

            If i = 0 Then
                max = dataGridView1.Rows(i).Cells(2).Value
                min = dataGridView1.Rows(i).Cells(2).Value
            End If

            If max < dataGridView1.Rows(i).Cells(2).Value Then
                max = dataGridView1.Rows(i).Cells(2).Value
            End If

            If min > dataGridView1.Rows(i).Cells(2).Value Then
                min = dataGridView1.Rows(i).Cells(2).Value
            End If

        Next

        avg = sum / dataGridView1.Rows.Count()

        TextBoxSum.Text = sum.ToString()
        TextBoxMax.Text = max.ToString()
        TextBoxMin.Text = min.ToString()
        textBoxAvg.Text = avg.ToString("00.00")

    End Sub
End Class
      
///////////////OUTPUT:

vb.net datagridview column max min sum average value
vb.net datagridview column max min sum average value




VB.NET - How To Get The DataGridView Column Average Value Using VB NET


VB DataGridView Column Avg Value

VB.NET - How To Get The Average Value Of A DataGridView Column In VB NET

                                                                                                                         

In This VB.NET Tutorial  We Will See How To Get The Average Value From Specific DataGridView Column And Display It In TextBox Using VBNET Programming Language.


Project Source Code:

Public Class VB_Datagridview_Column_Cells_Average_Value

    Private Sub VB_Datagridview_Column_Cells_Min_Average_Load(sender As Object, e As EventArgs) Handles MyBase.Load


       ' populate datagridview with some data
        Dim rand As New Random()
        For i As Integer = 0 To 11 Step +1
            dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), rand.Next(20, 65).ToString())
        Next
        dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
        dataGridView1.AllowUserToAddRows = False

        ' Method 1
        textBoxAvg.Text = (From row As DataGridViewRow In dataGridView1.Rows
        Where row.Cells(2).FormattedValue.ToString() <> String.Empty
                     Select Convert.ToInt32(row.Cells(2).FormattedValue)).Average().ToString()

        ' Method 2
        Dim avg As Double
        Dim sum As Integer
        For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step +1
            sum = sum + dataGridView1.Rows(i).Cells(2).Value
        Next

        avg = sum / dataGridView1.Rows.Count()
        textBoxAvg.Text = avg.ToString("00.000")
    End Sub
End Class
      
///////////////OUTPUT:


Vb.NET DataGridView Column Average Value
VB.NET DataGridView Column Avg Value