Home All Groups Group Topic Archive Search About

Bounded Datagrid and Subtotal rows

Author
29 Mar 2006 2:13 PM
josepm
Hello! Does anybody know how to add a subtotal row in a bounded datagrid?

Thanks!

Author
29 Mar 2006 3:10 PM
Phillip Williams
If you are using ASP.NET1.1, and you want a subtotal by page, then one way of
doing it would be to set the ShowFooter="True"on the datagrid and then to
calculate the page subtotal during the ItemDataBound event, e.g.

<asp:DataGrid ID="datagrid1" Runat="server" AutoGenerateColumns="False"
        AllowPaging="True" PageSize="5" ShowFooter="True" >
    <Columns>
        <asp:TemplateColumn HeaderText="Sale Price">
            <ItemTemplate   >
                <asp:Label id="lblSalePrice" Runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "SalePrice")%>'></asp:Label>
            </ItemTemplate>
            <FooterTemplate>
                <asp:Label ID="lblTotal" Runat="server"></asp:Label>
            </FooterTemplate>
        </asp:TemplateColumn>
    </Columns>
</asp:DataGrid>

    Private Sub DG_ItemDataBound(ByVal sender As Object, ByVal e As _
         System.Web.UI.WebControls.DataGridItemEventArgs) Handles
datagrid1.ItemDataBound
        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem) Then
            TotalSales += CType(e.Item.DataItem, DataRowView)("SalePrice")
        End If
        If ((e.Item.ItemType = ListItemType.Footer)) Then
            Dim lbl As Label = CType(e.Item.FindControl("lblTotal"), Label)
            If Not lbl Is Nothing Then
                lbl.Text = "Total " & TotalSales.ToString("$#.00")
            End If
        End If
    End Sub

Show quoteHide quote
"josepm" wrote:

> Hello! Does anybody know how to add a subtotal row in a bounded datagrid?
>
> Thanks!
Author
29 Mar 2006 3:32 PM
josepm
Thanks Phillip!

You have focus the answer about a "grand total", but my goal is to obtain
subtotals grouping by a datagrid column. In any case, I take note about your
code!!!

Thank you!

Show quoteHide quote
"Phillip Williams" wrote:

> If you are using ASP.NET1.1, and you want a subtotal by page, then one way of
> doing it would be to set the ShowFooter="True"on the datagrid and then to
> calculate the page subtotal during the ItemDataBound event, e.g.
>
> <asp:DataGrid ID="datagrid1" Runat="server" AutoGenerateColumns="False"
>         AllowPaging="True" PageSize="5" ShowFooter="True" >
>     <Columns>
>         <asp:TemplateColumn HeaderText="Sale Price">
>             <ItemTemplate   >
>                 <asp:Label id="lblSalePrice" Runat="server" Text='<%#
> DataBinder.Eval(Container.DataItem, "SalePrice")%>'></asp:Label>
>             </ItemTemplate>
>             <FooterTemplate>
>                 <asp:Label ID="lblTotal" Runat="server"></asp:Label>
>             </FooterTemplate>
>         </asp:TemplateColumn>
>     </Columns>
> </asp:DataGrid>
>
>     Private Sub DG_ItemDataBound(ByVal sender As Object, ByVal e As _
>          System.Web.UI.WebControls.DataGridItemEventArgs) Handles
> datagrid1.ItemDataBound
>         If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
> ListItemType.AlternatingItem) Then
>             TotalSales += CType(e.Item.DataItem, DataRowView)("SalePrice")
>         End If
>         If ((e.Item.ItemType = ListItemType.Footer)) Then
>             Dim lbl As Label = CType(e.Item.FindControl("lblTotal"), Label)
>             If Not lbl Is Nothing Then
>                 lbl.Text = "Total " & TotalSales.ToString("$#.00")
>             End If
>         End If
>     End Sub
>
> --
> HTH,
> Phillip Williams
> http://www.societopia.net
> http://www.webswapp.com
>
>
> "josepm" wrote:
>
> > Hello! Does anybody know how to add a subtotal row in a bounded datagrid?
> >
> > Thanks!