datagrid 用select from  where 语句查询出来的数据 已经不是整个数据库的数据,那么我想对当前显示的这一列里面的数据求和 并显示到Text中 ,怎么实现?

解决方案 »

  1.   

    select sum(my_field) from my_table where
      

  2.   


    问题就是 my_field 怎么取  比如我点在A列 那么 A列的字段名该如何传递进去?
      

  3.   

    也可以从头到尾计算while not rs.eof
        sumT=sumT + rs.fields("my_field")
        rs.movenext
      

  4.   


    我现在只有一个 TEXT 有很多列 Text 里面显示对哪一列的求和取决于我鼠标 位于那一列上 这一列并不是固定的
      

  5.   


    Private Sub DataGrid1_HeadClick(ByVal ColIndex As Integer)
        Dim i As Integer
        Dim sumT As Long
        
        DataGrid1.Col = ColIndex
        
        For i = 0 To Adodc1.Recordset.RecordCount - 1
            DataGrid1.Row = i
            sumT = sumT + DataGrid1.Text
        Next i
        
        Debug.Print sumT
        
    End Sub
      

  6.   


        DataGrid1.Col = ColIndex 这个是什么意思?
    怎么把结果显示在TEXT中呢? 谢谢 我新手
      

  7.   

    Private Sub DataGrid1_HeadClick(ByVal ColIndex As Integer)
        Dim i As Integer
        Dim sumT As Long
        
        DataGrid1.Col = ColIndex
        
        For i = 0 To Adodc1.Recordset.RecordCount - 1
            DataGrid1.Row = i
            sumT = sumT + DataGrid1.Text
        Next i
        
        text1.text= sumT
        
    End Sub
      

  8.   

    sumT = sumT + DataGrid1.Text 编译是错误 这句话“类型不匹配”
      

  9.   

    Dim sumT As Long
    **************
    改成你的类型,比如double
      

  10.   

    sumT = sumT + DataGrid1.Text 编译是错误 这句话“类型不匹配”
    我帮你调试出来了:
    通过val 把字符转换一下,就通用了统计所有行有数值的不报错误代码如下:
    Private Sub DataGrid1_HeadClick(ByVal ColIndex As Integer)
        Dim i As Integer
        Dim sumT As double
        
        DataGrid1.Col = ColIndex
        
        For i = 0 To Adodc1.Recordset.RecordCount - 1
            DataGrid1.Row = i
            sumT = val(sumT) +val( DataGrid1.Text)                         '改转换一下字符
        Next i
        
        text1.text= sumT
        
    End Sub