Link to home
Start Free TrialLog in
Avatar of joekus
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
Avatar of naveenkohli
naveenkohli

For this you will need a custom solution. Look at the following implementation of a tree like grid that has the features of rolling up data from child nodes to parent node.

http://www.netomatix.com/Products/ServerControls/TreeGrid.aspx
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(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles drgRentalArrears.ItemDataBound

      If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
          Dim dTotal As System.Decimal = Convert.ToDecimal(DataBinder.Eval(e.Item.DataItem, "price_a"))
          mTotalPayableAmount += dTotal
      ElseIf (e.Item.ItemType = ListItemType.Footer) Then
          Dim lblTemp As Label = CType(e.Item.FindControl("lblTotalAmount1"), Label)
          If Not lblTemp Is Nothing Then
            lblTemp.Text = String.Format("${0:##,###.##}", mTotalPayableAmount)
          End If
      End If

End Sub


Hope this helps
ASKER CERTIFIED SOLUTION
Avatar of iboutchkine
iboutchkine

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of joekus

ASKER

Hello,

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