public void kraGridView_RowDataBound(object sender, GridViewRowEventArgs e)

        int totalPercent = 0;
      if (e.Row.RowType == DataControlRowType.DataRow)
      {
          Label lblWeigh = (Label)e.Row.FindControl("lblWeigh");
         // totalPercent += Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "lblWeigh"));
         int total = int.Parse(lblWeigh.Text);
        totalPercent += total;
       }
      else if (e.Row.RowType == DataControlRowType.Footer)
      {
          Label lblTotalPercentage = (Label)e.Row.FindControl("lblTotalPercentage");
         // e.Row.Cells[5].Text = totalPercent.ToString();
          lblTotalPercentage.Text = totalPercent.ToString(); 
      }
}

Recommended Answers

Can you elaborate a bit more on what you need help with? Do you have a specific question, or is there an error you are having a problem with?

Jump to Post

This MSDN Article/Tutorial explains how to build a gridview with a footer that includes summary/calculations. Exactly what you are trying to do.

Displaying Summary Information in the GridView's Footer

Is this the article that you followed? If so, were you able to …

Jump to Post

All 6 Replies

Can you elaborate a bit more on what you need help with? Do you have a specific question, or is there an error you are having a problem with?

im trying to calculate a column in grid view, i'm retrieving the data from the database and i'm trying to get the sum of the column and place it on the grid view footer, now the code above is not calculating the sum

this here is my asp.net

<asp:GridView ID="kraGridView" runat="server" AutoGenerateColumns="False" 
                                        BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" 
                                        onprerender="kraGridView_PreRender" onrowcreated="kraGridView_RowCreated" 
                                        ShowFooter="True" Width="806px" onrowdatabound="kraGridView_RowDataBound">
                                        <Columns>
                                            <asp:BoundField DataField="Measure" FooterText="Grand Total" 
                                                HeaderText="Measure" />
                                            <asp:BoundField DataField="Indicator" HeaderText="Indicator" />
                                            <asp:BoundField DataField="Objective" HeaderText="Objective" />
                                            <asp:BoundField DataField="Description" HeaderText="Description" />

                                            <asp:TemplateField HeaderText ="Weighting" >
                                                <ItemTemplate>
                                                    <asp:Label ID="lblWeigh" runat="server" Text='<%# Eval("Weighting") %>'/>
                                                </ItemTemplate>
                                                <FooterTemplate>
                                                    <asp:Label ID ="lblTotalPercentage" runat="server" />
                                                </FooterTemplate>
                                            </asp:TemplateField>

                                        </Columns>

                                        <FooterStyle BackColor="White" Font-Bold="True" ForeColor="Black"/>
                                        <HeaderStyle Font-Size="12pt" ForeColor="White" />
                                        <RowStyle BorderStyle="Double" />
                                    </asp:GridView>

This MSDN Article/Tutorial explains how to build a gridview with a footer that includes summary/calculations. Exactly what you are trying to do.

Displaying Summary Information in the GridView's Footer

Is this the article that you followed? If so, were you able to get it working at all?

Yeah, and other articles, but i cant seem to get the sum of my "Weighting" Total on the footer

Hey Guys, finally got it, with the code below, thanks for the assistance

public partial class _Default : System.Web.UI.Page 
{
    decimal grdTotal = 0;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
protected void GridView1_RowDataBound
                   (object sender, GridViewRowEventArgs e)
{
 if (e.Row.RowType == DataControlRowType.DataRow)
 {
  decimal rowTotal = Convert.ToDecimal
              (DataBinder.Eval(e.Row.DataItem, "Amount"));
  grdTotal = grdTotal + rowTotal;
 }
 if (e.Row.RowType == DataControlRowType.Footer)
 {
  Label lbl = (Label)e.Row.FindControl("lblTotal");
  lbl.Text = grdTotal.ToString("c");
 }
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.