locked
Find total for a column in Repeater RRS feed

  • Question

  • User1182587605 posted

    I have a column price which shows dollars in it. In a custom created textbox in footer of the repeater, I want to show the Total in dollars for the column.

    I need to add all the column values in the price column of repeater and show it in a textbox. Please give me a code on how to do it. A working sample would do as I will make changes to my code.

    Thanks,

    Deepak

    Monday, November 21, 2016 6:43 PM

Answers

  • User283571144 posted

    Hi acmedeepak,

    Now, I need to populate the total values in a textbox(TxtTotalValue) outside the Repeater. Can you please help me in this.

    According to your description, I suggest you could try another solution.

    You could firstly bind the data to the Repeater.

    Then you could foreach the Repeater's rows to get the total values and add it.

    At last, you could use fill the result to the TxtTotalValue textbox.

    More details, you could refer to follow codes:

    Html:

      <div>
                <table>
                <asp:Repeater ID="Repeater1" runat="server"  >
                    <ItemTemplate>
                        <tr>
                           <td>
                                <asp:TextBox ID="TxtItemid" Text='<%# Eval("ItemId")%>'  class="form-control" runat="server" Width="150"></asp:TextBox>
                            </td>
                             <%--<td>
                                <asp:TextBox ID="TxtitemName" class="form-control" Visible='<%# Eval("ItemId") != "" %>' runat="server" Text='<%# Eval("ItemName")%>' Width="300"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtqty" runat="server" Text='<%# Eval("Qnty")%>' Width="150" Visible='<%# Eval("ItemId") != "" %>' class="form-control"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtcost" class="form-control" Style="text-align: right" Visible='<%# Eval("ItemId") != "" %>' runat="server" Text='<%# Eval("Cost")%>' Width="150" ReadOnly="True"> </asp:TextBox>
                            </td>--%>
                            <td>
                                <asp:TextBox ID="Txttotal" runat="server"  Style="text-align: right" class="form-control" Width="150" Text='<%# Eval("CalVal")%>' ReadOnly="True"></asp:TextBox>
                            </td>
                           <%-- <td>&nbsp&nbsp&nbsp
                            </td>
                            <td>
                                <asp:ImageButton runat="server" class="delet-icon" ID="Imagebutton1" CommandName="DeleteRow" OnClick="imgbtndelete_Click" Visible='<%# Eval("ItemId") != "" %>' src="images/icon-delete.png"></asp:ImageButton>
    
                            </td>
                            <td>&nbsp&nbsp&nbsp
                            </td>
                            <td>
                                <asp:Button runat="server" ID="Button3" Text="Update Row" CommandName="UpdateRow" class="btn btn-dark-blue" Visible='<%# Eval("ItemId") != "" %>' OnClick="Button3_Click"></asp:Button>
                            </td>--%>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
                    </table>
    
                <hr/>
                Total:<asp:TextBox ID="Ttotal" runat="server"></asp:TextBox>
            </div>

    Code-behind:

      protected void Page_Load(object sender, EventArgs e)
            {
                DataTable d1 = new DataTable();
                d1.Columns.Add("ItemId");
                d1.Columns.Add("CalVal", typeof(decimal));
                d1.Rows.Add("1", 20.32);
                d1.Rows.Add("2", 30.43);
                d1.Rows.Add("3", 40.54);
                d1.Rows.Add("4", 50.54);
                d1.Rows.Add("5", 60.74);
                Repeater1.DataSource = d1;
                Repeater1.DataBind();
    
                decimal d2 = 0;
                //Here you could calculate the totale value
                foreach (RepeaterItem item in Repeater1.Items)
                {
                    //to get the textbox of each line
                    TextBox Te = (TextBox)item.FindControl("Txttotal");
                    string re = Te.Text;
                    //convert string to decimal
                    d2 += Convert.ToDecimal(re);
    
                }
                Ttotal.Text = d2.ToString();
    
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 24, 2016 7:29 AM

All replies

  • User283571144 posted

    Hi acmedeepak,

    I have a column price which shows dollars in it. In a custom created textbox in footer of the repeater, I want to show the Total in dollars for the column.

    I need to add all the column values in the price column of repeater and show it in a textbox. Please give me a code on how to do it. A working sample would do as I will make changes to my code.

    According to your description, I suggest you could use LinQ select and sum method to get the total price value.

    Then you could use find control method to find the textbox in the footertemplate.

    At last, you could fill the value into the textbox.

    More details, you could refer to follow codes:

       <div>
            <asp:Repeater ID="Repeater1" runat="server" >
               <HeaderTemplate>
            <table  border="1">
                <tr>
                    <th scope="col" style="width: 80px">
                        Id
                    </th>
                    <th scope="col" style="width: 120px">
                        Price
                    </th>
                </tr>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblCustomerId" runat="server" Text='<%# Eval("Id") %>' />
                </td>
                <td>
                    <asp:Label ID="lblContactName" runat="server" Text='<%# Eval("Price") %>' />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
              <tr>
                <td>
                  Total:
                </td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            </table>
        </FooterTemplate>
            </asp:Repeater>
        </div>

    Code-behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                Bind();
            }
    
            private void Bind()
            {
                //I use datatable instead of search the database
                DataTable d1 = new DataTable();
                d1.Columns.Add("Id");
                d1.Columns.Add("Price", typeof(int));
                d1.Rows.Add("1", 20);
                d1.Rows.Add("2", 30);
                d1.Rows.Add("3", 40);
                d1.Rows.Add("4", 50);
                d1.Rows.Add("5", 60);
                Repeater1.DataSource = d1;
                Repeater1.DataBind();
                //Get the totoal value use LinQ
                int totalPrice = d1.Select().Sum(p => Convert.ToInt32(p["Price"]));
                //Fill the result into textbox
                (Repeater1.Controls[Repeater1.Controls.Count - 1].Controls[0].FindControl("TextBox1") as TextBox).Text = totalPrice.ToString();
            }

    Result:

    Best Regards,

    Brando

    Tuesday, November 22, 2016 3:00 AM
  • User1182587605 posted

    Hi Brando,

    can you please give me a total value sample on decimal points in the following scenario. I need to find the total of all the Total elements displayed in the repeater. Please look for TxtTotal in there:

    ASPX code:

    <asp:Repeater ID="Repeater1" runat="server" OnItemCommand="Repeater1_ItemCommand">
    <ItemTemplate>
                                              <tr> 
    
    
     <td>
                                       <asp:TextBox ID="TxtItemid" Text='<%# Eval("ItemId")%>' Visible='<%# Eval("ItemId") != "" %>' class="form-control" runat="server"  Width="150"  ></asp:TextBox>
                                         </td>
                                                   <td>
                                                      <asp:TextBox ID="TxtitemName" class="form-control" Visible='<%# Eval("ItemId") != "" %>' runat="server" Text='<%# Eval("ItemName")%>'  Width="300" ></asp:TextBox>
                                                    </td>
                                              <td>
                <asp:TextBox ID="Txtqty" runat="server" Text='<%# Eval("Qnty")%>' Width="150" Visible='<%# Eval("ItemId") != "" %>' class="form-control" ></asp:TextBox>
                          </td>                 <td>             
                                             <asp:TextBox ID="Txtcost" class="form-control" style="text-align: right" Visible='<%# Eval("ItemId") != "" %>' runat="server" Text='<%# Eval("Cost")%>' Width="150" ReadOnly="True"> </asp:TextBox>
                                               </td>
                                              <td>        
                <asp:TextBox ID="Txttotal" runat="server" Visible='<%# Eval("ItemId") != "" %>' style="text-align: right" class="form-control"  Width="150" Text='<%# Eval("CalVal")%>'  ReadOnly="True" ></asp:TextBox>
                                         
        </td>
                                                   <td>
                                                      &nbsp&nbsp&nbsp
                                                  </td>
                                               
                                                   <td >
     <asp:imagebutton runat="server"  class="delet-icon" ID="Imagebutton1" CommandName ="DeleteRow" onclick="imgbtndelete_Click" Visible='<%# Eval("ItemId") != "" %>' src="images/icon-delete.png" ></asp:imagebutton>
                                                            
                                                   </td>
                                                   <td>
                                                      &nbsp&nbsp&nbsp
                                                  </td>
                                                   <td>
                            <asp:button runat="server" ID="Button3" Text="Update Row" CommandName="UpdateRow" class="btn btn-dark-blue" Visible='<%# Eval("ItemId") != "" %>'  OnClick="Button3_Click" ></asp:button>
                             </td>
                                                   
    </tr>
    
                                              </ItemTemplate>
    </asp:Repeater>

    aspx.cs:

     protected void Page_PreRender(object sender, EventArgs e)
            {
    
                if (!IsPostBack)
                {
                    GetData();
                    Repeater1.DataSource = dt;
                    Repeater1.DataBind();
                }
                string CurrTNumber = TxtInventoryNumber.Text;
                TxtInventoryNumber.Text = CurrTNumber;
                string SQLtotal;
                //SQLtotal = "select SUM(Qty)as TotalQty, SUM(Qty*Cost)as Total from IC_Inventory_T where Invy_Num = '" + CurrTNumber + "'";
                SQLtotal = "select SUM(Qty)as TotalQty, cast(SUM(Qty*Cost) as decimal(14,2)) as Total from IC_Inventory_T where Invy_Num  = '" + CurrTNumber + "'";
                SqlConnection con = new SqlConnection(ConString);
                //SqlCommand tcmd = new SqlCommand(SQLtotal, con);
    
                SqlDataReader RestCmd;
                SqlCommand SeltCmd = new SqlCommand(SQLtotal, con);
    
    
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                if (CurrTNumber != "")
                {
    
                    RestCmd = SeltCmd.ExecuteReader();
                    if (RestCmd.Read())
                    {
                        txtTotalValue.Text = Convert.ToString(RestCmd["Total"]);
                        txtTotalQuantity.Text = Convert.ToString(RestCmd["TotalQty"]);
                    }
                }
                con.Close();
                con.Dispose();
    
    
            }

    Now, I need to populate the total values in a textbox(TxtTotalValue) outside the Repeater. Can you please help me in this.

    Regards,

    Deepak

    Wednesday, November 23, 2016 11:25 AM
  • User283571144 posted

    Hi acmedeepak,

    Now, I need to populate the total values in a textbox(TxtTotalValue) outside the Repeater. Can you please help me in this.

    According to your description, I suggest you could try another solution.

    You could firstly bind the data to the Repeater.

    Then you could foreach the Repeater's rows to get the total values and add it.

    At last, you could use fill the result to the TxtTotalValue textbox.

    More details, you could refer to follow codes:

    Html:

      <div>
                <table>
                <asp:Repeater ID="Repeater1" runat="server"  >
                    <ItemTemplate>
                        <tr>
                           <td>
                                <asp:TextBox ID="TxtItemid" Text='<%# Eval("ItemId")%>'  class="form-control" runat="server" Width="150"></asp:TextBox>
                            </td>
                             <%--<td>
                                <asp:TextBox ID="TxtitemName" class="form-control" Visible='<%# Eval("ItemId") != "" %>' runat="server" Text='<%# Eval("ItemName")%>' Width="300"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtqty" runat="server" Text='<%# Eval("Qnty")%>' Width="150" Visible='<%# Eval("ItemId") != "" %>' class="form-control"></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="Txtcost" class="form-control" Style="text-align: right" Visible='<%# Eval("ItemId") != "" %>' runat="server" Text='<%# Eval("Cost")%>' Width="150" ReadOnly="True"> </asp:TextBox>
                            </td>--%>
                            <td>
                                <asp:TextBox ID="Txttotal" runat="server"  Style="text-align: right" class="form-control" Width="150" Text='<%# Eval("CalVal")%>' ReadOnly="True"></asp:TextBox>
                            </td>
                           <%-- <td>&nbsp&nbsp&nbsp
                            </td>
                            <td>
                                <asp:ImageButton runat="server" class="delet-icon" ID="Imagebutton1" CommandName="DeleteRow" OnClick="imgbtndelete_Click" Visible='<%# Eval("ItemId") != "" %>' src="images/icon-delete.png"></asp:ImageButton>
    
                            </td>
                            <td>&nbsp&nbsp&nbsp
                            </td>
                            <td>
                                <asp:Button runat="server" ID="Button3" Text="Update Row" CommandName="UpdateRow" class="btn btn-dark-blue" Visible='<%# Eval("ItemId") != "" %>' OnClick="Button3_Click"></asp:Button>
                            </td>--%>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater>
                    </table>
    
                <hr/>
                Total:<asp:TextBox ID="Ttotal" runat="server"></asp:TextBox>
            </div>

    Code-behind:

      protected void Page_Load(object sender, EventArgs e)
            {
                DataTable d1 = new DataTable();
                d1.Columns.Add("ItemId");
                d1.Columns.Add("CalVal", typeof(decimal));
                d1.Rows.Add("1", 20.32);
                d1.Rows.Add("2", 30.43);
                d1.Rows.Add("3", 40.54);
                d1.Rows.Add("4", 50.54);
                d1.Rows.Add("5", 60.74);
                Repeater1.DataSource = d1;
                Repeater1.DataBind();
    
                decimal d2 = 0;
                //Here you could calculate the totale value
                foreach (RepeaterItem item in Repeater1.Items)
                {
                    //to get the textbox of each line
                    TextBox Te = (TextBox)item.FindControl("Txttotal");
                    string re = Te.Text;
                    //convert string to decimal
                    d2 += Convert.ToDecimal(re);
    
                }
                Ttotal.Text = d2.ToString();
    
            }

    Result:

    Best Regards,

    Brando

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, November 24, 2016 7:29 AM