Solved

DataGrid Cart Footer Sum with Recalculate Feature?

Posted on 2006-07-15
1
323 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

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…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

634 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