从datagridview计算数据

时间:2013-09-30 05:05:49

标签: vb.net datagridview access-vba vb.net-2010 datagridviewcolumn

我一直试图找到解决这个问题的方法,但我一直无法自己提出解决方案或在网上找到解决方案。

我的问题如下:我在vb.net 2010中创建了一个应用程序我有一个连接到项目的Access数据库。该数据库有2个表,一个是代表,另一个是演示。我正在尝试提供有关代表的一些有用的统计数据,如总演示,总销售额(CPO),总建议,总演示和已售出演示总数。我已经成功地让前三个工作,但没有得到最后的2.这一个的问题是输入到该字段的数据是一个简单的是或否我不知道如何输入代码表示如果单元格的值为Yes,则向计数器添加加1,如果将No加1加到另一个计数器。

以下是我现在使用的代码,适用于前3个统计信息。

'To calculate the total demos when you change the Representative
    Dim row As Cutco_testDataSet.DemosRow

    Dim intTotalItems As Integer
    For Each row In Cutco_testDataSet.Demos.Rows
        intTotalItems += 1
    Next
    lblTotalItems.Text = intTotalItems.ToString

    'Declare the variables for the values to be stored and later displayed
    Dim intTotalCPO As Integer
    Dim intTotalRecs As Integer

    'to look in Demosdgv and add the corresponding data
    For i As Integer = 0 To Cutco_testDataSet.Tables(0).Rows.Count - 1
        intTotalCPO += Cutco_testDataSet.Tables(0).Rows(i).Item("CPO")
        intTotalRecs += Cutco_testDataSet.Tables(0).Rows(i).Item("Rec's")
    Next
    lblTotalCpo.Text = intTotalCPO.ToString("c0")
    lblTotalRecs.Text = intTotalRecs.ToString()

当值为null时我也遇到麻烦它告诉我它不能添加DBNull。 无论如何,如果尚未输入值(null)或仅将null计为0,那么计数器只会继续并忽略。

提前致谢!

1 个答案:

答案 0 :(得分:0)

我正在重写您的代码:

'Dim row As Cutco_testDataSet.DemosRow

'Dim intTotalItems As Integer
'For Each row In Cutco_testDataSet.Demos.Rows
'    intTotalItems += 1
'Next
'No need to use above "for loop" for counting total rows.  
lblTotalItems.Text = Cutco_testDataSet.Demos.Rows.Count

'Declare the variables for the values to be stored and later displayed
Dim intTotalCPO As Integer
Dim intTotalRecs As Integer
dim intTotdemosgiven as Integer

'to look in Demosdgv and add the corresponding data
'For i As Integer = 0 To Cutco_testDataSet.Tables(0).Rows.Count - 1
For Each row as Datarow in Cutco_testDataSet.Tables(0).Rows
    intTotalCPO += NullToZero(row("CPO"))
    intTotalRecs += NullToZero(row("Rec's"))

    intTotdemosgiven = intTotdemosgiven + IIf(UCase(nulltostring(row("DemosGiven"))) = "YES", 1, 0) 'I dont know exact field name for Demos given. 

Next
lblTotalCpo.Text = intTotalCPO.ToString("c0")
lblTotalRecs.Text = intTotalRecs.ToString()



Public Function NullToZero(ByRef FieldVal) As Double
'This function converts null or blank values to zero.
    If FieldVal Is Nothing OrElse FieldVal.ToString() = vbNullString Then
        Return 0
    Else
        Return FieldVal
    End If
End Function

Public Function NullToString(ByRef FieldVal) As String
    If FieldVal Is Nothing OrElse IsDBNull(FieldVal) Then
        Return vbNullString
    Else
        Return FieldVal
    End If
End Function

希望这有帮助