How to display a totals line in the GridView & DataGrid footer
‘Case Gridview
Dim dTotal As Decimal = 0
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
If e.Row.RowType = DataControlRowType.DataRow Then
dTotal += Convert.ToDecimal(DataBinder.Eval(e.Row.DataItem, “price“))
End If
If e.Row.RowType = DataControlRowType.Footer Then
e.Row.Cells(1).Text = “Totals:“
e.Row.Cells(2).Text = dTotal.ToString(“c“)
e.Row.Cells(1).HorizontalAlign = HorizontalAlign.Right
e.Row.Cells(2).HorizontalAlign = HorizontalAlign.Right
e.Row.Font.Bold = True
End If
End Sub
‘Case DataGrid
Dim dTotal As Double = 0Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
If e.Item.ItemType = ListItemType.Item Then
dTotal = dTotal + Convert.ToDouble(e.Item.Cells(0))
End If
If e.Item.ItemType = ListItemType.Footer Then
e.Item.Cells(1).Text = dTotal.ToString()
End If
End Sub