给你源码 Private total As Double = 0 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '在此处放置初始化页的用户代码 If Not Page.IsPostBack Then bindgrid() End If End Sub Sub bindgrid() Dim cn As SqlConnection = New SqlConnection(connection1.connstr) Dim da As SqlDataAdapter = New SqlDataAdapter("select top 10 customerid,freight from orders", cn) Dim ds As New DataSet() da.Fill(ds) DataGrid1.DataSource = ds DataGrid1.DataBind() End Sub Sub calctotal(ByVal lz As String) Try total += Double.Parse(lz) Catch End Try End Sub Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then calctotal(e.Item.Cells(1).Text) e.Item.Cells(1).Text = String.Format("{0:c}", Convert.ToDouble(e.Item.Cells(1).Text)) ElseIf e.Item.ItemType = ListItemType.Footer Then e.Item.Cells(0).Text = "合计" e.Item.Cells(1).Text = String.Format("{0:c}", total) End If End Sub
我喜欢写存储过程所以这些都是在SQL那边构造的
DataRow dr=ds.Tables["dt"].NewRow();创建一空行,这一行的列值就设置成你刚才各列加起来的值。大概是这个意思吧,你试试
Private total As Double = 0
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'在此处放置初始化页的用户代码
If Not Page.IsPostBack Then
bindgrid()
End If
End Sub
Sub bindgrid()
Dim cn As SqlConnection = New SqlConnection(connection1.connstr)
Dim da As SqlDataAdapter = New SqlDataAdapter("select top 10 customerid,freight from orders", cn)
Dim ds As New DataSet()
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataBind() End Sub Sub calctotal(ByVal lz As String)
Try
total += Double.Parse(lz)
Catch End Try
End Sub Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.Item Then
calctotal(e.Item.Cells(1).Text)
e.Item.Cells(1).Text = String.Format("{0:c}", Convert.ToDouble(e.Item.Cells(1).Text))
ElseIf e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(0).Text = "合计"
e.Item.Cells(1).Text = String.Format("{0:c}", total)
End If
End Sub
{
if(e.Item.ItemType==ListItemType.AlternatingItem||
e.Item.ItemType==ListItemType.Item )
{
caltotal(e.Item.Cells[1].Text);
e.Item.Cells[1].Text=string.Format("{0:c}", Convert.ToDouble (e.Item.Cells[1].Text));
}
else if(e.Item.ItemType==ListItemType.Footer)
{
e.Item.Cells[0].Text="合计";
e.Item.Cells[1].Text = string.Format("{0:c}", total);
}