2017-08-07 12 views
0

特定の列からのdatagridview行のコンテンツの合計を計算するにはどうすればよいですか?vb.netのdatagridview内の特定の列のRow値の合計を計算する方法

これは私のdatagridviewです。

Name  Score 
John  35 
Helen  34 
James  30 

Total  99 

私は列の合計を計算したい "スコア"をテキストボックスに入れてください。

ありがとうございました。私は、これはあなたを助けるだろうと思い

Private Sub GetSUMofUnits() 
    Dim total As Integer 

    For Each row As DataGridViewRow In dgvSubjectsEnrolled.Rows 
     total += row.Cells(4).Value 
    Next 
    txtUnits.Text = total 
End Sub 
+0

https://social.msdn.microsoft.com/Forums/azure/en-US/44ddc6cf-09e5-4f77-abb5-85650a2cad70/how-to-calculate-total-amount-in-datagridview-このリンクをお試しくださいcolumn-and-row-in-vbnet?フォーラム= vbgeneral – Prathyush

答えて

0
Imports System.Data.SqlClient 
Public Class Form1 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim connetionString As String 
     Dim connection As SqlConnection 
     Dim adapter As New SqlDataAdapter 
     Dim ds As New DataSet 
     Dim i,total As Integer 
     total = 0 
     connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password" 
     connection = New SqlConnection(connetionString) 
     Try 
      connection.Open() 
      adapter.SelectCommand = New SqlCommand("SELECT * FROM your_DB_table", connection) 
      adapter.Fill(ds) 
      connection.Close() 
      For i = 0 To ds.Tables(0).Rows.Count - 1 
       total = total + ds.Tables(0).Rows(i).Item(1) 
      Next 
     Catch ex As Exception 
      MsgBox(ex.ToString) 
     End Try 
     textbox1.text = total 
    End Sub 
End Class 

:このような

0

何か、

Dim sumOfScore As Integer = 0 
For Each row As DataRow in *yourDataRow* 
    Dim score As Integer = CInt(row.Item("Score").ToString()) 
    sumOfScore += score 
Next 
txtYourTextBox.Text = sumOfScore.ToString 
0

また、私は私の問題を解決し、このコードを得ました。

関連する問題