Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


how to calculate average/mean of Rows in datagridview instead of columns in c# (Visual studio 2015)

Cody DriewCody Driew Member Posts: 1

there isn't any codes yet, i just added my database (MS Access incase you needed to know)
im doing some codes for the pageant. this is so far the GUI.
so, here....
i should get the average of Casual Attire, Talent, Swimsuit, Gown, and question and answer.
i dont have any idea how to insert any codes.. im still a begginer in the programming world.!!


  • Priyasha SarmahPriyasha Sarmah Member Posts: 14

    This program can be used to find the average, sum, min and max of datagrid view column in c#:
    namespace WindowsFormsApplication1
    public partial class Csharp_datagridview_max_min_average_sum : Form
    public Csharp_datagridview_max_min_average_sum()

     private void Csharp_datagridview_max_min_average_sum_Load(object sender, EventArgs e)
            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();


Sign In or Register to comment.