Solved

DataGrid Cart Footer Sum with Recalculate Feature?

Posted on 2006-07-15
1
282 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
1 Comment
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 500 total points
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

AJAX ModalPopupExtender has a required property "TargetControlID" which may seem to be very confusing to new users. It means the server control that will be extended by the ModalPopup, for instance, if when you click a button, a ModalPopup displays,…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now