Solved

DataGrid Cart Footer Sum with Recalculate Feature?

Posted on 2006-07-15
1
321 Views
Last Modified: 2012-06-21
Thanks to EE I found this very informative link concerning add a DataGrid "Total" column to it's footer.

http://www.dotnetjunkies.com/Tutorial/2F527E21-A6C5-497A-8B56-4150BDAF711D.dcik


Now, most shopping carts I've seen have a "Recalculate" button so a use can change the order quantity.

I've attempted to do this but have failed. Help..........

Here's their code with my home-brew code:

[ASP] (abbreviated for space)
<asp:DataGrid id="DataGrid1" runat="server" OnItemDataBound="DataGrid1_ItemDataBound" ShowFooter="True">


[C# Code Behind]
    private string total;
    private int iQuantity;
    private double runningTotal = 0;

public void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            TextBox t = (TextBox)e.Item.Cells[4].Controls[1]; //3
            iQuantity = int.Parse(t.Text);
            decimal unitprice = decimal.Parse(e.Item.Cells[3].Text);
            total = total + (unitprice * iQuantity);
           
            CalcTotal(total); //(e.Item.Cells[3].Text);
            e.Item.Cells[3].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Cells[3].Text));
        }
        else if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.Cells[2].Text = "Total";
            e.Item.Cells[3].Text = string.Format("{0:c}", runningTotal);
        }
    }

    private void CalcTotal(string _price)
    {
        try
        {
            runningTotal += Double.Parse(_price);
        }
        catch
        {
            //A value was null
        }
    }
0
Comment
Question by:kvnsdr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 500 total points
ID: 17115074
Add a button "Recalculate" near the control whitch reflects the total. In this button's click event do the folowing:
 - cycle through datagrid's rows and, if needed, update cart's info in database (at least your link sample uses SQL); and don't forget to do error-checking (like zero values, text values, blank fields, etc.)
 - rebind datagird; this way you'll get new value in a total field as ItemDataBound event will be fired again

HTH
Ramuncikas
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question