locked
Calauclate Total In Footer Based On Selected Row Values RRS feed

  • Question

  • User2033107836 posted

    HI..

    This is my aspx page

     <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
                <Columns>
    <asp:TemplateField>
                               <HeaderTemplate>
                    <asp:CheckBox ID="chkHeader" runat="server" />
                </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
    
       <asp:TemplateField HeaderText="QPP" SortExpression="QPP">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("QPP") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" runat="server" Text='<%# Bind("QPP") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle ForeColor="Teal" />
                    </asp:TemplateField>
      </Columns>
            </asp:GridView>
    

    I'm getting Output like this

    Check Box	Value
    		10
    		20
    		5	
    Grand Total	25

    For example User check Second Row and Third Row for that sum is 25

    Wednesday, February 22, 2017 8:33 AM

Answers

  • User-1509636757 posted

    Use GridView.FooterRow.FindControl() to get the label from Footer Row and assign the text:

    double sum = 0.0;
    foreach (GridViewRow r in GridView2.Rows)
    {
        if (r.RowType == DataControlRowType.DataRow)
        {
            if ((r.FindControl("chkSelect") as CheckBox).Checked)
                sum += Convert.ToDouble((r.FindControl("LAmount") as Label).Text);
        }
    }
    (GridView2.FooterRow.FindControl("GrandTotal") as Label).Text = sum.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2017 11:13 AM

All replies

  • User-1509636757 posted

    You can write jQuery on change event of chkSelect to fetch all Label7 value and sum it to display in a Label. Here is similar example:

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.12.4.min.js"></script>
        <script>
            $(document).ready(function () {
                $(".chkSelect, .chkHeader").change(function () {
                    var sum = 0;
                    $(".chkSelect :checked").each(function () {
                        sum += parseFloat($(this).closest('td').next().find(".Label7").text());
                    });
                    $(".lblTotal").text(sum);
                })
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:CheckBox ID="chkHeader" runat="server" CssClass="chkHeader" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server" CssClass="chkSelect" />
                        </ItemTemplate>
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="QPP" SortExpression="QPP">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("QPP") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" CssClass="Label7" runat="server" Text='<%# Bind("QPP") %>'></asp:Label>
                        </ItemTemplate>
                        <HeaderStyle ForeColor="Teal" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            Grand Total: <asp:Label ID="lblTotal" CssClass="lblTotal" runat="server" />
        </form>
    </body>
    </html>

    I have added proper CssClass names to Label and CheckBox controls to ease in jQuery.

    Wednesday, February 22, 2017 9:22 AM
  • User2033107836 posted

    @kaushalparik...

    my gridview is in updatepanel will this jquery works?

    Wednesday, February 22, 2017 9:40 AM
  • User-1509636757 posted

    Yes, it should. However, you need to retain this value if you want to have it displayed as it is, between postbacks, since because it is being set from client side, the Grand Total label may get empty on next postback/partial postback.

    Wednesday, February 22, 2017 9:43 AM
  • User2033107836 posted

    is any method do in C# code behind

    Coz on postback i want to remain value same what user selected

    Wednesday, February 22, 2017 9:54 AM
  • User-1509636757 posted

    One easy hack is, to save sum value in a HiddenField also and assign it to label again in C# code behind event where postback is happening (or best place is where grid is being bind - but that depends on your code scenario):

    <!DOCTYPE html>
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="Scripts/jquery-1.12.4.min.js"></script>
        <script>
            $(document).ready(function () {
                $(".chkSelect, .chkHeader").change(function () {
                    var sum = 0;
                    $(".chkSelect :checked").each(function () {
                        sum += parseFloat($(this).closest('td').next().find(".Label7").text());
                    });
                    $(".lblTotal").text(sum);
                    $("#hdnTotal").val(sum);
                })
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" ShowFooter="true" GridLines="None">
                <Columns>
                    <asp:TemplateField>
                        <HeaderTemplate>
                            <asp:CheckBox ID="chkHeader" runat="server" CssClass="chkHeader" />
                        </HeaderTemplate>
                        <ItemTemplate>
                            <asp:CheckBox ID="chkSelect" runat="server" CssClass="chkSelect" />
                        </ItemTemplate>
                        <FooterTemplate>
                            Test1
                        </FooterTemplate>
                        <FooterStyle BorderStyle="None" />
                        <ItemStyle BorderStyle="Solid" />
                        <HeaderStyle BorderStyle="Solid" />
                    </asp:TemplateField>
    
                    <asp:TemplateField HeaderText="QPP" SortExpression="QPP">
                        <EditItemTemplate>
                            <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("QPP") %>'></asp:TextBox>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label7" CssClass="Label7" runat="server" Text='<%# Bind("QPP") %>'></asp:Label>
                        </ItemTemplate>
                        <FooterTemplate>
                            Test2
                        </FooterTemplate>
                        <FooterStyle BorderStyle="None" />
                        <ItemStyle BorderStyle="Solid" />
                        <HeaderStyle BorderStyle="Solid" ForeColor="Teal" />
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
            Grand Total:
            <asp:Label ID="lblTotal" CssClass="lblTotal" runat="server" />
            <asp:HiddenField runat="server" ID="hdnTotal" />
        </form>
    </body>
    </html>
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!Page.IsPostBack)
                    BindGridView();
                lblTotal.Text = hdnTotal.Value;
            }



    Wednesday, February 22, 2017 10:05 AM
  • User2033107836 posted
    double sum = 0.0;
    foreach (GridViewRow r in GridView2.Rows)
    {
    
    if (r.RowType == DataControlRowType.DataRow)
    {
    
    if ((r.FindControl("chkSelect") as CheckBox).Checked)
    {
    
    sum += Convert.ToDouble((r.FindControl("LAmount") as Label).Text);
    
    }
    
    }
    
    Label10.Text = sum.ToString();
    
    }



    I used this above code in aspx page it works fine if i use Label10

    But I want to show in my gridview footertemplate contraol GrandTotal
    Instead of Label10 how to give my footwer template label 

    Wednesday, February 22, 2017 10:49 AM
  • User-1509636757 posted

    Use GridView.FooterRow.FindControl() to get the label from Footer Row and assign the text:

    double sum = 0.0;
    foreach (GridViewRow r in GridView2.Rows)
    {
        if (r.RowType == DataControlRowType.DataRow)
        {
            if ((r.FindControl("chkSelect") as CheckBox).Checked)
                sum += Convert.ToDouble((r.FindControl("LAmount") as Label).Text);
        }
    }
    (GridView2.FooterRow.FindControl("GrandTotal") as Label).Text = sum.ToString();

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, February 22, 2017 11:13 AM