C# - How To Get The Average, Max, Min Of A Column In The dataGridView Using C#

c# datagridview column max min sum average value

C# - How To Get The Average, Max, Min, Sum Of A DataGridView Column Values In C#

                                                                                                                                                     

In This C# utorial  We Will See How To Get The Average, Max, Min, Sum Value From Specific DataGridView Column And Display Them In TextBoxes Using C# Programming Language.


Project Source Code:


using System;
using System.Collections;
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_max_min_average_sum : Form
    {
        public Csharp_datagridview_max_min_average_sum()
        {
            InitializeComponent();
        }

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


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

            // Sum Value
            textBoxSum.Text = columnData.Sum().ToString();
            // Max Value
            textBoxMax.Text = columnData.Max().ToString();
            // Min Value
            textBoxMin.Text = columnData.Min().ToString();
            // Average Value
            textBoxAvg.Text = columnData.Average().ToString();
        }
    }
}


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

c# datagridview column sum average max min value
c# datagridview column max min sum average value



Share this

Related Posts

Previous
Next Post »