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 _ItemDataB ound" ShowFooter="True">
[C# Code Behind]
private string total;
private int iQuantity;
private double runningTotal = 0;
public void DataGrid1_ItemDataBound(ob ject sender, DataGridItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingIt em)
{
TextBox t = (TextBox)e.Item.Cells[4].C ontrols[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.Ce lls[3].Tex t));
}
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
}
}
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
[C# Code Behind]
private string total;
private int iQuantity;
private double runningTotal = 0;
public void DataGrid1_ItemDataBound(ob
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingIt
{
TextBox t = (TextBox)e.Item.Cells[4].C
iQuantity = int.Parse(t.Text);
decimal unitprice = decimal.Parse(e.Item.Cells
total = total + (unitprice * iQuantity);
CalcTotal(total); //(e.Item.Cells[3].Text);
e.Item.Cells[3].Text = string.Format("{0:c}", Convert.ToDouble(e.Item.Ce
}
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.