hi,
Im using a repeater control bound to a database table. everything works fine. i need to display the total of a column from that database table in the footer template of my repeater control.
but im clueless as to how it can b done. Any help would be appreciated.

thanks.

Dani AI

Generated

asked how to show a column total in a Repeater. correctly noted that COUNT(*) returns a row count while SUM(column) returns the numeric total; demonstrated a server-side accumulation approach. The notes below outline several alternative ways to get a correct, maintainable total and when to pick each.

  • Database-side aggregate (recommended for large sets or paging): run a query that returns the total from the database, e.g. SELECT SUM(Amount) AS GrandTotal FROM Orders WHERE .... This is fastest and guarantees the total reflects all rows, not only the current page.
  • Window function (when supported): include SUM(Amount) OVER() in the select to return the grand total alongside each row so the bound data already contains the total.
  • In-memory aggregate (small result sets): compute the total from the fetched rows, for example dt.Compute("SUM(Amount)", "") or with LINQ dt.AsEnumerable().Sum(r => r.Field<decimal>("Amount")).
  • Separate scalar query: run one query for the rows and a second SELECT SUM(...) if the app flow prefers two simple operations.

Place a placeholder in the Repeater footer and set its text after the chosen aggregation is available (either set it after the scalar query, or after computing from the in-memory result). That keeps display logic simple and avoids recalculating during item rendering.

Practical notes: use decimal for money to avoid floating-point rounding, guard against DBNull when summing, format the final number for the current culture, and prefer DB aggregation for performance or when totals must reflect all rows (not just the current page).

Recommended Answers

All 2 Replies

hi,
Im using a repeater control bound to a database table. everything works fine. i need to display the total of a column from that database table in the footer template of my repeater control.
but im clueless as to how it can b done. Any help would be appreciated.

thanks.

Do you need to show SELECT COUNT(*) FROM dbo.TableName ?

There are a number of different methods you could employ to arrive at the total, but here is a base which includes one such method.

<asp:Repeater ID="rptrDemo" runat="server" OnItemDataBound="rptrDemo_ItemDataBound">
            <ItemTemplate>
                <asp:Literal ID="litValue" runat="server" /><br />
            </ItemTemplate>
            <FooterTemplate>
                <br />
                <strong>Total:</strong><br />
                <asp:Literal ID="litTotal" runat="server" />
            </FooterTemplate>
        </asp:Repeater>
protected void Page_Load(object sender, EventArgs e)
    {
        List<double> values = new List<double>() { 15.0, 100.0, 250.0, 125.50 };
        DataTable dt = new DataTable();
        dt.Columns.Add("TestValue");

        foreach (double d in values)
        {
            DataRow dr = dt.NewRow();
            dr["TestValue"] = d;
            dt.Rows.Add(dr);
        }
        
        rptrDemo.DataSource = dt;
        rptrDemo.DataBind();
    }

    protected void rptrDemo_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item
            || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // retrieve underlying datarow and get value
            DataRow dr = ((DataRowView)e.Item.DataItem).Row;
            double val = Convert.ToDouble(dr["TestValue"]);

            // find literal and display value
            Literal litValue = (Literal)e.Item.FindControl("litValue");
            litValue.Text = val.ToString("0.00");

            // add to running total
            this.RunningTotal += val;
        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
            // find literal and display total
            Literal litTotal = (Literal)e.Item.FindControl("litTotal");           
            litTotal.Text = this.RunningTotal.ToString("0.00");
        }
    }

    private double RunningTotal { get; set; }
Be a part of the DaniWeb community

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