目的在表尾添加统计行,计算各行的和,代码如下:
Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim n1 As Decimal = 0.0
        Dim n2 As Integer = 0
        If e.Row.RowIndex >= 0 Then
            n1 += Convert.ToDouble((e.Row.Cells(3).Text).ToString)
            Response.Write(n1)   '这里显示的结果是字符串相加,而不是数值相加,郁闷!
        End If
        'If e.Row.RowType = DataControlRowType.DataRow Then
        '    Dim myrows As DataRowView = CType(e.Row.DataItem, DataRowView)
        '    n1 = n1 + CType(myrows(3).ToString(), Integer)
        '    Response.Write(n1)  '这里显示的结果也是字符串相加,而不是数值相加,郁闷!
        '    n2 = Convert.ToInt32(myrows(4).ToString())
        '    n1 += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, "df_cb"))
        'End If
        If e.Row.RowType = DataControlRowType.Footer Then            e.Row.Cells(0).Text = "合计"
            e.Row.Cells(3).Text = n1.ToString  '这里显示的总是0,而没有将各行相加。
            
        End If
    End Sub,
在使用过程中,e.Row.Cells(3).Text 始终显示为0,用Response.Write(n1)显示的内容为“500600”,数据库中的数据为500和600,两数值未相加仅作了字符连接,请教各位高手帮助,谢谢!(请用VB语言)

解决方案 »

  1.   

    每一次绑定时候,n1都被清空。肯定有问题。  需定义全局变量。并且每个的值需要转换为double型的,在相加。
      

  2.   

    GridView1_RowDataBound 有多少行就会执行多少次....  你可以考虑在数据库里进行统计
      

  3.   

    请给个详细的代码,谢谢!
    为什么Response.Write(n1)  显示的结果是字符串相连,而不是和?
      

  4.   

    Private sum As Double = 0 
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 
        If e.Row.RowIndex >= 0 Then 
            sum += Convert.ToDouble(e.Row.Cells(3).Text) 
        ElseIf e.Row.RowType = DataControlRowType.Footer Then 
            e.Row.Cells(2).Text = "合计:" 
            e.Row.Cells(3).Text = sum.ToString() 
        End If 
    End Sub 
      

  5.   

    Dim n1 As Decimal = 0.0 每次执行时都为0.跟踪看看就知道了
      

  6.   

    或者
    datatable dt=数据源
    decimal sum=0
    for(int i;i<dt.Rows.Count;i++)
    {
    if (dt.Rows[i]["需要统计的列"].text.tostring!="")
    {
    sum+=Convert.ToDecimel(dt.Rows[i]["需要统计的列"]);
    }
    }
    dt.Rows.Add(添加一行数据,需要显示的合计的显示,。其他的为null);
    gv.dataSounce=dt;
    gv.databind();