Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////How To Get The Sum Of DataGridView Column Values Using VB NET
- ///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
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement