0

我有这个代码过滤并显示数据库到datagridview

前任。

score | scoretotal
25    | 30
20    | 40
25    | 25

现在,我怎样才能得到测验的总数和“scoretotal”的总数,或者我应该说,如何得到只有 datagridview 显示的总数?

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

任何意见和建议将不胜感激

4

1 回答 1

1

林克:

 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"))
于 2013-10-06T08:29:43.447 回答