VB.NET - How To Get The Sum Of DataGridView Column Values Using VB NET

VB NET Sum Of DataGridView Column Values


VB.NET - How To Get The Sum Of All DataGridView Column Values In VB NET

                                                                                                                         

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


Project Source Code:


Public Class VB_Datagridview_Column_Cells_Values_Sum

    Private Sub VB_Datagridview_Column_Cells_Values_Sum_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
        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()

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

        textBoxSum.Text = sum.ToString()

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





vb.net datagridview column get sum values
vb.net datagridview column get sum values



Share this

Related Posts

Previous
Next Post »

1 comments:

comments
28 avril 2017 à 03:13 delete

amateur de vb.net :
merci beaucoup.....

Reply
avatar