joekus
asked on
Summing rows for datagrid columns.
Is there a way to show a row of sums for the the values in the columns above them in a datagrid?
TIA
JK
TIA
JK
You can put the sum in each column footer.
Assuming the table field name is "price_a", declare a private variable mTotalPrice and do the sum up in the DataGrid ItemDataBound event.
[aspx]
<asp:datagrid id="DataGrid1" AutoGenerateColumns="False " runat="server" ShowFooter="True">
<Columns>
<asp:TemplateColumn HeaderText="Payable Amount">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.price_a", "${0:N}") %>
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblTotalAmount1" Runat="server"></asp:Label >
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
[aspx.vb]
Private mTotalPrice As System.Decimal = 0
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mTotalPayableAmount = 0
End Sub
Private Sub DataGrid1_ItemDataBound(By Val sender As Object, ByVal e As System.Web.UI.WebControls. DataGridIt emEventArg s) Handles drgRentalArrears.ItemDataB ound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt em) Then
Dim dTotal As System.Decimal = Convert.ToDecimal(DataBind er.Eval(e. Item.DataI tem, "price_a"))
mTotalPayableAmount += dTotal
ElseIf (e.Item.ItemType = ListItemType.Footer) Then
Dim lblTemp As Label = CType(e.Item.FindControl(" lblTotalAm ount1"), Label)
If Not lblTemp Is Nothing Then
lblTemp.Text = String.Format("${0:##,###. ##}", mTotalPayableAmount)
End If
End If
End Sub
Hope this helps
Assuming the table field name is "price_a", declare a private variable mTotalPrice and do the sum up in the DataGrid ItemDataBound event.
[aspx]
<asp:datagrid id="DataGrid1" AutoGenerateColumns="False
<Columns>
<asp:TemplateColumn HeaderText="Payable Amount">
<ItemTemplate>
<%# DataBinder.Eval(Container,
</ItemTemplate>
<FooterTemplate>
<asp:Label id="lblTotalAmount1" Runat="server"></asp:Label
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>
[aspx.vb]
Private mTotalPrice As System.Decimal = 0
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mTotalPayableAmount = 0
End Sub
Private Sub DataGrid1_ItemDataBound(By
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingIt
Dim dTotal As System.Decimal = Convert.ToDecimal(DataBind
mTotalPayableAmount += dTotal
ElseIf (e.Item.ItemType = ListItemType.Footer) Then
Dim lblTemp As Label = CType(e.Item.FindControl("
If Not lblTemp Is Nothing Then
lblTemp.Text = String.Format("${0:##,###.
End If
End If
End Sub
Hope this helps
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello,
My apologies. I am learning to be clearer. Am using datagrid in standalone app, so asp is not applicable.
My apologies. I am learning to be clearer. Am using datagrid in standalone app, so asp is not applicable.
My code will work on Win apps
http://www.netomatix.com/Products/ServerControls/TreeGrid.aspx