Tuesday, November 15, 2011

Show Total in GridView Footer

 Given Example shows you how to calculate total of particular cells value and show at the footer of Gridview.
At first define footer template in template field for which you want to calculate total.

<asp:TemplateField HeaderText="Total Pcs">
          <asp:Label ID="lbltotpcs" runat="server"  Text='<%# Bind("totpcs") %>'></asp:Label>
        Total : <asp:Label ID="lbltotqty" runat="server" Text="Label"></asp:Label>

Populate the RowDataBound event of Gridview where you have to do the main calculation.

int totqty=0;
protected void gvCrate_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label totpcs = (Label)e.Row.FindControl("lbltotpcs");
            totqty = totqty + (Convert.ToInt32(totpcs.Text));
        if (e.Row.RowType == DataControlRowType.Footer)
            ((Label)e.Row.FindControl("lbltotqty")).Text = totqty.ToString();
            TxtTotalpcs.Value = totqty.ToString();

Enable  "ShowFooter" as True in Gridview's property.

No comments:

Post a Comment