Adds

Wednesday, 11 December 2013

Add GridView Column Total in ASP.NET C#

Grid View Style(Make ShowFooter = true & Add Footer Template)

<asp:GridView ID="gvCommissionDetails" runat="server" ShowFooter="true" CssClass="gridview"
        AutoGenerateColumns="False"
        onrowdatabound="gvCommissionDetails_RowDataBound">
    <AlternatingRowStyle CssClass="gridview_alter"/>
        <Columns>
            <asp:TemplateField HeaderText="CommissionDate">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("CommissionDate") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server"
                        Text='<%# Bind("CommissionDate", "{0:dd-MMM-yyyy}") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ClientCode">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ClientCode") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("ClientCode") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="FA">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("FA") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblFA" runat="server" Text='<%# Bind("FA") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblFATotal" runat="server"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="UM">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("UM") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblUM" runat="server" Text='<%# Bind("UM") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblUMTotal" runat="server"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="CO">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("CO") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="lblCO" runat="server" Text='<%# Bind("CO") %>'></asp:Label>
                </ItemTemplate>
                <FooterTemplate>
                    <asp:Label ID="lblCOTotal" runat="server"></asp:Label>
                </FooterTemplate>
            </asp:TemplateField>
           
        </Columns>

    </asp:GridView>

Code in .cs page


decimal totalFA = 0M;
        decimal totalUM = 0M;
        decimal totalCO = 0M;

        protected void gvCommissionDetails_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {               
                decimal FACommission = Convert.ToDecimal(((Label)e.Row.FindControl("lblFA")).Text);
                totalFA += FACommission;

                decimal UMCommission = Convert.ToDecimal(((Label)e.Row.FindControl("lblUM")).Text);
                totalUM += UMCommission;

                decimal COCommission = Convert.ToDecimal(((Label)e.Row.FindControl("lblCO")).Text);
                totalCO += COCommission;
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                Label TotalFACommission = (Label)e.Row.FindControl("lblFATotal");
                TotalFACommission.Text = totalFA.ToString();

                Label TotalUMCommission = (Label)e.Row.FindControl("lblUMTotal");
                TotalUMCommission.Text = totalUM.ToString();

                Label TotalCOCommission = (Label)e.Row.FindControl("lblCOTotal");
                TotalCOCommission.Text = totalCO.ToString();
            }
        }