Search code examples
c#asp.netgridviewfooter

Show total of every column in footer within a gridview


Hello I want to show each column total for every column 7 to 18. With this code it gives me the total of all the columns and shows it in every cell of the footer. I want to show each column total in the equivalent footer cell. I am using asp:BoundFields in asp.

example: column1 sum =2 ,column2 sum =3, column3 sum=4, with this code gives me the same total 2+3+4=9 to all the cells of the footer.I want to show in the footer.cell[1] =2,footer.cell[2] =3,footer.cell[4] =4 (programmatically). The total for each column and not the full total of all columns.
Any suggestions?

private int TotalStats = (int)0;
 protected void GridViewHomeTeamStats_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 7; i <= 18; i++){
        // check row type
        if (e.Row.RowType == DataControlRowType.DataRow)
            // if row type is DataRow, add ...                
        {
            TotalStats += Convert.ToInt32(e.Row.Cells[i].Text);
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
            // If row type is footer, show calculated total value
            e.Row.Cells[i].Text = String.Format("{0:0.##}", TotalStats);
            }
    }

Thank you.Can you help me also with these three columns?In these columns the values are like: 3/4 ,5/6 etc.I want to show in the equivalent footer cell the sum and divide of this columns. Example if a column has 2 rows with values 1/2 , 2/4 I want to show 1+2/2+4=0.5=50% in the footer cell . this is the asp code that is from the same GridViewHomeTeamStats :

 <asp:GridView ID="GridViewHomeTeamStats" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceHomeTeam" ShowFooter="True" OnRowDataBound="GridViewHomeTeamStats_RowDataBound">
    <Columns>            
        <asp:TemplateField HeaderText="FT" SortExpression="FREE_THROW_MADE">
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("FREE_THROW_MADE") + "/" + Eval("FREE_THROW_ATTEMPT") %>'></asp:Label></ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="2P" SortExpression="2POINTS_MADE">
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("2POINTS_MADE") + "/" + Eval("2POINTS_ATTEMPT") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="3P" SortExpression="3POINT_MADE">
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("3POINT_MADE") + "/" + Eval("3POINT_ATTEMPT") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

Solution

  • Try this (probably I would rather use decimal):

    int[] TotalStats = new int[12] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
    
    protected void GridViewHomeTeamStats_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        for (int i = 7; i <= 18; i++)
        {
            // check row type
            if (e.Row.RowType == DataControlRowType.DataRow)
            // if row type is DataRow, add ...                
            {
                TotalStats[i-7] += Convert.ToInt32(e.Row.Cells[i].Text);
            }
            else if (e.Row.RowType == DataControlRowType.Footer)
                // If row type is footer, show calculated total value
                e.Row.Cells[i].Text = String.Format("{0:0.##}", TotalStats[i-7]);
        }
    }