Sabtu, 02 April 2016

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

Bagikan

Jangan lewatkan

VB.NET - How To Get The DataGridView Column Average Value Using VB NET
4/ 5
Oleh

Subscribe via email

Suka dengan artikel di atas? Tambahkan email Anda untuk berlangganan.