Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataGrid Cart Footer Sum with Recalculate Feature?

Posted on 2006-07-15
1
Medium Priority
?
336 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
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…
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.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

722 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