Advertisement
bakhcha

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

Apr 30th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 1.24 KB | None | 0 0
  1. ////How To Get The Sum Of DataGridView Column Values Using VB NET
  2.  
  3. ///Project Source Code:
  4.  
  5.  
  6. Public Class VB_Datagridview_Column_Cells_Values_Sum
  7.  
  8.     Private Sub VB_Datagridview_Column_Cells_Values_Sum_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  9.  
  10.        ' populate datagridview with some data
  11.         Dim rand As New Random()
  12.         For i As Integer = 0 To 11 Step +1
  13.             dataGridView1.Rows.Add("First Name" + i.ToString(), "Last Name" + i.ToString(), rand.Next(20, 65).ToString())
  14.         Next
  15.         dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
  16.         dataGridView1.AllowUserToAddRows = False
  17.  
  18.         ' Method 1
  19.         textBoxSum.Text = (From row As DataGridViewRow In dataGridView1.Rows
  20.         Where row.Cells(2).FormattedValue.ToString() <> String.Empty
  21.                Select Convert.ToInt32(row.Cells(2).FormattedValue)).Sum().ToString()
  22.  
  23.         ' Method 2
  24.         Dim sum As Integer = 0
  25.         For i As Integer = 0 To dataGridView1.Rows.Count() - 1 Step +1
  26.             sum = sum + dataGridView1.Rows(i).Cells(2).Value
  27.         Next
  28.  
  29.         textBoxSum.Text = sum.ToString()
  30.  
  31.     End Sub
  32. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement