Display Sum of Column Total in Repeater Footer row in ASP.Net

Last Reply on Mar 17, 2015 12:01 AM By Azim

Posted on Mar 15, 2015 11:26 PM

Hi,

I have repeater control and price fileds inside it i want to have a sum of all prices row on footer of repeater.

please guidde.

Posted on Mar 17, 2015 12:01 AM Modified on on Mar 17, 2015 12:01 AM

Hi nauna,

Please refer this code.

HTML

<asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <th>SubjectName</th>
                <th>Marks</th>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><asp:Label ID="lblSubjectName" Text='<%#Eval("SubjectName") %>' runat="server" /></td>
            <td><asp:Label ID="lblMarks" Text='<%#Eval("Marks") %>' runat="server" /></td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        <tr>
            <td>Total</td>
            <td><asp:Label ID="lblTotal" runat="server" /></td>
        </tr>
        </table>
    </FooterTemplate>
</asp:Repeater>

Namespaces

C#

using System.Data;

 VB.Net

Imports System.Data

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DataTable dt = new DataTable();
        dt.Columns.AddRange(new DataColumn[2] { 
                    new DataColumn("SubjectName", typeof(string)),
                    new DataColumn("Marks",typeof(int)) });
        dt.Rows.Add("English", 56);
        dt.Rows.Add("Hindi", 75);
        dt.Rows.Add("Maths", 61);
        dt.Rows.Add("Science", 79);
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
        int totalMarks = dt.Select().Sum(p => Convert.ToInt32(p["Marks"]));
        (Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("lblTotal") as Label).Text = totalMarks.ToString();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim dt As DataTable = New DataTable()
        dt.Columns.AddRange(New DataColumn(1) {
                            New DataColumn("SubjectName", GetType(String)),
                            New DataColumn("Marks", GetType(Integer))})
        dt.Rows.Add("English", 56)
        dt.Rows.Add("Hindi", 75)
        dt.Rows.Add("Maths", 61)
        dt.Rows.Add("Science", 79)
        Repeater1.DataSource = dt
        Repeater1.DataBind()
        Dim totalMarks As Integer = dt.[Select]().Sum(Function(p) Convert.ToInt32(p("Marks")))
        TryCast(Repeater1.Controls(Repeater1.Controls.Count - 1).Controls(0).FindControl("lblTotal"), Label).Text = totalMarks.ToString()
    End If
End Sub

Output

SubjectNameMarks
English 56
Hindi 75
Maths 61
Science 79
Total 271