目的在表尾添加统计行,计算各行的和,代码如下:
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语言)
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语言)
为什么Response.Write(n1) 显示的结果是字符串相连,而不是和?
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
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();