[AS 2.0] Display shopping Cart Items in Flash Datagrid

Hello Community,

I have an online shopping cart website that I am trying to create with various products. I’m in search of displaying the user selections of items in a flash datagrid in the order the product was selected. Currently I can see the user selections however, they are not in the order the items were chosen. For example if I only select one item that item should not be in the 3rd place in the datagrid it should be at the top of the datagrid. I have searched various fourms on finding a solution for this but no luck. Any help is greatly apprecitated! Thanks in advance


import mx.controls.*;
//import mx.controls.Alert;
import fl.controls.dataGridClasses.*;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.data.DataProvider;
import mx.data.binding.*;
import fl.controls.ScrollPolicy;
class Cart2 {
	var CartCalc:String;
	var quantity:NumericStepper;
	var Product:Number;
	var Product2:Number;
	var mynum:String;
	var CartTotal:Number = 0;
	var CartTotal2:Number = 0;
	var CalcMe:Number;
	var Total:Number = 0;
	var myArr:Object;
	var myArr2:Object;
	var myArr3:Object;
	var m_parent_mc:MovieClip;
	var GiftCardsText:String;
	var PostersText:String;
	var cart:DataGrid;
	var cart_dg:DataGrid;
	var data_array = new Array();
	var count:Number;
	//Gift Card Item
	public function GetUserSelection() {
		var quantity = _level0.TESTMC.myvalue.value;
		Product = 5.20;
		var CalculateValue = (Product*quantity*100)/100;
		var num_str2:String = String(CalculateValue);
		_global.myArr = new Array();
		myArr = {Quantity:quantity, Product:"Gift Cards", Price:"5.00", Total:CalculateValue};
		for (var i:Number = 0; i<myArr.length; i++) {
			var Quantity:String = myArr[i][0];
			var Productname:String = myArr[i][1];
			var Price:String = myArr[i][2];
			var Total:String = myArr[i][3];
		}
		//trace("CalcValue"+ CalculateValue);
		drawCartTotal(CalculateValue);
	}
	// Post Card Item
	public function GetU() {
		var quantity = _level0.TESTMC.myvalue2.value;
		Product2 = 10.00;
		CalcMe = (Product2*quantity*100)/100;
		_global.myArr2 = new Array();
		myArr2 = {Quantity:quantity, Product:"Post Cards", Price:"10.00", Total:CalcMe};
		//trace(myArr2);
		drawCartTotal(CalcMe);
		for (var i:Number = 0; i<myArr2.length; i++) {
			var Quantity:String = myArr2[i][0];
			var Productname:String = myArr2[i][1];
			var Price:String = myArr2[i][2];
			var Total:String = myArr2[i][3];
			//trace("For Loop 2" + myArr2);
		}
		//trace("CalcMe"+ CalcMe);
	}
	public function GetCandyValue() {
		var quantity = _level0.TESTMC.CandyStepper.value;
		//trace(quantity);
		Product = 1.00;
		var CandyValue = (Product*quantity*100)/100;
		//var num_str2:String = String(CandyValue);  
		_global.myArr3 = new Array();
		myArr3 = {Quantity:quantity, Product:"Gift Cards", Price:"5.00", Total:CandyValue};
		for (var i:Number = 0; i<myArr3.length; i++) {
			var Quantity:String = myArr3[i][0];
			var Productname:String = myArr3[i][1];
			var Price:String = myArr3[i][2];
			var Total:String = myArr3[i][3];
		}
		//trace("CandyValue"+ CandyValue);
		drawCartTotal(CandyValue);
	}
	//Calculates Total Price
	function drawCartTotal(MyTotal:Number) {
		Total += MyTotal;
		_level0.CartTotal = String(Total);
		_level0.CartTotal.indexOf(".") == -1 ? _level0.CartTotal += ".00" : _level0.CartTotal += "0";
	}
	//Check Out function to display Datagrid with user selections
	public function CheckOut() {
		var thisCart:Cart2 = this;
		// dynamically add a DataGrid to the Stage and give it an instance name of "cart_dg".
		cart = _level0.createClassObject(mx.controls.DataGrid, "cart_dg", 0);
		var src:EndPoint = new EndPoint();
		src.component = cart_dg;
		src.property = "value";
		src.event = "change";
		//trace (cart_dg);
		//Set up Columns
		cart.columnNames = ["Quantity", "Product", "Price", "Total"];
		cart.setSize(500, 800);
		var name_dgc = cart.getColumnAt(0);
		name_dgc.width = 100;
		name_dgc.headerText = "Quantity";
		var pn = cart.getColumnAt(1);
		pn.width = 150;
		pn.headerText = "Product";
		var price_dgc = cart.getColumnAt(2);
		price_dgc.width = 80;
		price_dgc.headerText = "Price";
		var cartTotal = cart.getColumnAt(3);
		cartTotal.width = 70;
		cartTotal.headerText = "Total";
		cart.move(40, 220);
		cartTotal.labelFunction = function(item:Object):String  {
			if ((item.Price != undefined) && (item.Quantity != undefined)) {
				//trace("Test"+item.Price)
				var cartTotal:Number = Math.round(item.Price*item.Quantity*100)/100;
				var num_str:String = String(cartTotal);
				//trace("Num Str"+ num_str);
				var num_array = num_str.split(".");
				if (num_array[1] == undefined) {
					num_array[1] = "00";
				}
				if (num_array[1].length == 1) {
					num_array[1] += "0";
				}
				return "$"+(num_array.join("."));
			}
		};
		//Create a dataProvider to add some data to the Datagrid
		data_array = new Array();
		//data_array.push(myArr, myArr2, myArr3);
		data_array.addItem(myArr);
		data_array.addItem(myArr2);
		data_array.addItem(myArr3);
		cart.dataProvider = data_array;
		
		//Listener set up to listen for user clicks on the datagrid & prompt them with an option of deleting the item selected		 
		var deleteProductListener:Object = new Object();
		deleteProductListener.change = function(evt:Object) {
			var myClickHandler = function (alertEvt:Object) {
				if (alertEvt.detail == Alert.OK) {
					//cart.dataProvider.removeItemAt(cart.selectedIndex)
					_level0.cart_dg.dataProvider.removeItemAt("");
					//trace(evt.target.selectedItem);
				}
			};
			Alert.show("Are you sure you want to delete this item?", "Remove Item From Cart", Alert.OK | Alert.CANCEL, thisCart.m_parent_mc, myClickHandler, "stockIcon", Alert.OK);
		};
		cart.addEventListener("change", deleteProductListener);
	
	//Update Cart Total after item has been deleted from Datagrid
	
	}
}