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.

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.