Julie
Julie

Reputation: 333

How to do calculation inside <asp:Gridview>

I have the following code to display list of data. I need to calculate the total.

<asp:GridView ID="gvS" runat="server" DataKeyNames="DateCheckIn" AutoGenerateColumns="false" AllowPaging="false" ShowFooter="true" CssClass="table table-striped table-bordered table-hover table-checkable dataTable no-footer" EmptyDataText="No bookings found." OnRowDataBound="gvS_RowDataBound">
    <FooterStyle Font-Bold="true" />
        <Columns>
            <asp:TemplateField HeaderText="Item" ItemStyle-Wrap="false" ItemStyle-Font-Size="12px">
                <ItemTemplate>
                    <%# Container.DataItemIndex + 1 %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField HeaderText="Check-In Date / Payment Date" DataField="DateCheckIn" DataFormatString="{0:dd-MMM-yyyy}" ItemStyle-Font-Size="12px" />
                <asp:TemplateField HeaderText="Room Sales" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lRoom" Text='<%# string.Format("{0:#,0.00}", Eval("Room")) %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="POS" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lPos" Text='<%# string.Format("{0:#,0.00}", Eval("Pos")) %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Shower" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lShower" Text='<%# string.Format("{0:#,0.00}", Eval("Shower")) %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Total" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
                    <ItemTemplate>
                        // Total = lRoom + lPos + lShower
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
</asp:GridView>

I need to calculate the Total (at the right side) which equal to lRoom + lPos + lShower. I manage to get Total for the footer.

The following is my gvS_rowDataBound

protected void gvS_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        Label lRoom = (Label)e.Row.FindControl("lRoom");
        Label lPos = (Label)e.Row.FindControl("lPos");
        Label lShower = (Label)e.Row.FindControl("lShower");

        decimal amount = 0;

        if (decimal.TryParse(lRoom.Text, out amount))
            totalRoomSales += amount;

        if (decimal.TryParse(lPos.Text, out amount))
            totalPosSales += amount;

        if (decimal.TryParse(lShower.Text, out amount))
            totalShowerSales += amount;

    }
    else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[2].Text = string.Format("{0:#,0.00}", totalRoomSales);
        e.Row.Cells[3].Text = string.Format("{0:#,0.00}", totalPosSales);
        e.Row.Cells[4].Text = string.Format("{0:#,0.00}", totalShowerSales);
     }
}

The output should be something like this

------------------------------------------------------------
| Item | Check-In Date | Room Sales | POS | Shower | Total |
------------------------------------------------------------
| 1    | 01/01/2019    |   100.00   | 5.00| 8.00   | 113.00|
| 2    | 02/01/2019    |    50.00   | 2.00| 3.50   |  55.50|
------------------------------------------------------------
|      |               |   150.00   | 7.00|11.50   | 168.50|
------------------------------------------------------------

Help me to get the total please.. Thanks

Upvotes: 0

Views: 133

Answers (1)

Mohsin Mehmood
Mohsin Mehmood

Reputation: 4236

Try something like this:

<asp:TemplateField HeaderText="Total" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="right" ItemStyle-Font-Size="12px" FooterStyle-HorizontalAlign="Right">
                    <ItemTemplate>
<asp:Label runat="server" ID="lTotal" Text='<%# string.Format("{0:#,0.00}", Convert.ToDouble(Eval("Room")) + Convert.ToDouble(Eval("Pos")) + Convert.ToDouble(Eval("Shower"))) %>'></asp:Label>

                    </ItemTemplate>
                </asp:TemplateField>

C#

if (e.Row.RowType == DataControlRowType.DataRow)
    {
             Label lTotal = (Label)e.Row.FindControl("lTotal");

            if (decimal.TryParse(lShower.Text, out amount))
                  totalSales+= amount;

    }
 else if (e.Row.RowType == DataControlRowType.Footer)
    {
        e.Row.Cells[2].Text = string.Format("{0:#,0.00}", totalRoomSales);
        e.Row.Cells[3].Text = string.Format("{0:#,0.00}", totalPosSales);
        e.Row.Cells[4].Text = string.Format("{0:#,0.00}", totalShowerSales);
        e.Row.Cells[5].Text = string.Format("{0:#,0.00}", totalSales);
     }

Upvotes: 1

Related Questions