Link to home
Start Free TrialLog in
Avatar of kvnsdr
kvnsdr

asked on

DataGrid Cart Footer Sum with Recalculate Feature?

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
        }
    }
ASKER CERTIFIED SOLUTION
Avatar of Ramuncikas
Ramuncikas
Flag of Lithuania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial