我有这个代码过滤并显示数据库到datagridview
离。
score | scoretotal
25 | 30
20 | 40
25 | 25
现在,如何获得测验总数和“得分总数”的总和,或者我应该说,如何获得仅显示数据网格视图的总数?
Private Sub datagrid()
Dim conn As New OleDbConnection
Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
Try
conn = New OleDbConnection(Get_Constring)
conn.Open()
cmd.Connection = conn
cmd.CommandType = CommandType.Text
sSQL = "SELECT score as score, total as total FROM prelimquiz"
sSQL = sSQL & " where username like '%" & Administrator.lblusername.Text & "%' and studentid like '%" & Me.Label25.Text & "%'"
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
Me.DataGridView1.DataSource = dt
If dt.Rows.Count = 0 Then
End If
Catch ex As Exception
MsgBox(ErrorToString)
Finally
conn.Close()
End Try
End Sub
任何建议和建议将不胜感激
答案 0 :(得分:1)
通过linq:
Dim dtAsEnum = dt.AsEnumerable
Dim ScoreResult = dtAsEnum.Sum(Function(row) row.Field(Of Integer)("score"))
Dim TotalResult = dtAsEnum.Sum(Function(row) row.Field(Of Integer)("total"))