Terry

Terry

  • NA
  • 148
  • 0

Find the sum of all values of a column in ListView

Mar 23 2015 5:20 PM

In my ListView, I have InsertTemplate : 
            <InsertItemTemplate>
<td>
<asp:DynamicControl runat="server" DataField="PercentageOfPayment" ID="PercentageOfPaymentIns" Mode="Insert" />
</td>
            </InsertItemTemplate>


In my InsertMethod, I want to iterate thru all the items of ListView & sum up the values of "PercentageOfPayment" field. If it exceeds 100, I want to warn the user. This is the code of my InsertMethod that shows various things I had tried for this :
        public void paymentList_InsertItem()
        {
            decimal percentagetotal = 0;

            if (currentProject == null)
                currentProject = _db.Projects.Find(projectId);

            var item = new VincitoreCRMApplication.Models.PaymentSchedule(projectId, projectName);

            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                              
                foreach (ListViewItem lvi in paymentList.Items)
                {
                    PaymentSchedule sc = (PaymentSchedule)lvi.DataItem;
                    if (sc != null)
                    {
                        System.Diagnostics.Debug.WriteLine("$$$$$$$$$ SC = " + sc.PercentageOfPayment);
                    }
                    else
                        System.Diagnostics.Debug.WriteLine("$$$$$$$$$ SC = null");

                    /*
                    //if (Decimal.TryParse(lvi.))
                    if (lvi.ItemType == ListViewItemType.DataItem)
                    {
                        System.Web.DynamicData.DynamicControl dc = paymentList.FindControl("PercentageOfPaymentIns") as System.Web.DynamicData.DynamicControl;
                        
                    }
                    percentagetotal += p.PercentageOfPayment;*/
                } 

                //paymentList.Items.Sum()
                if ((totalPercentage + item.PercentageOfPayment) > 100)
                {
                    System.Diagnostics.Debug.WriteLine("$$$$$$$$$ PERCENTAGE TOTAL Comes To " + percentagetotal + " .. NOT Valid > 100");
                }
                else
                {
                    // Save changes here
                    _db.PaymentScheduleOfProject.Add(item);
                    _db.SaveChanges();
                    paymentList.EditIndex = -1;
                }

            }
        }


I referred many sites, tried to iterate and find the sum, but couldn't achieve it. What am I missing over here, can you point out and help me solve it !! 

Any help is highly appreciated.

Thanks