# Datagrid calculate column - some useful stuff, some help needed!

**URL:** <https://forum.kirupa.com/t/datagrid-calculate-column-some-useful-stuff-some-help-needed/193435>\
**Category:** Uncategorized\
**Created:** [July 13, 2006, 10:57am UTC](https://forum.kirupa.com/t/datagrid-calculate-column-some-useful-stuff-some-help-needed/193435 "2006-07-13T10:57:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kilgore](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kilgore/32/2566_2.png) [@kilgore](https://forum.kirupa.com/u/kilgore)\
**Post date:** [July 13, 2006, 10:57am UTC](https://forum.kirupa.com/t/datagrid-calculate-column-some-useful-stuff-some-help-needed/193435/1 "2006-07-13T10:57:03Z")

</div>

Hi - hope someone on my favourite site can help with this datagrid issue - I think it’s just a small bit of code I’ve got to tweak. Might be some useful information to someone here anyway.

I’ve got a datagrid populated by info from mysql via amfphp.

I’ve managed to create a column that multiplies the qty and price fields:

```auto
root.dgCart.addColumn(column2);
		
		var column2 = new DataGridColumn();
		column2.headerText = "sum";
		
		column2.labelFunction = customLabelFunction2;
		column2.columnName = 'sum';

function customLabelFunction2(item:Object):Number {
		// if (item == undefined) return undefined;
  		var sumtotal = round2D(Number(item.item_price) * Number(item.qty));
		return sumtotal;
		}
		
		
		//helps to make decimals
		function round2D (nr){
	 	var tmp = String(nr)
	 	var pos = tmp.substr(tmp.indexOf(".")+1)
	 	if(tmp.indexOf(".")==-1) return nr+".00" //no decimals
	 	else if(pos.length==1) return nr+"0" //1 decimal
	 	else return Math.round (Number(nr) * 100) / 100 //2 or more
}

```

Now I’d like to get a total of all the items in the ‘sum’ column - the following code works to add up the ‘price’ column but the same approach doesn’t seem to work for the ‘sum’ column - perhaps because the figures are generated by a function?

```auto

function getTotal(){
		var totalPrice = 0;
		for(var i=0; i<root.dgCart.length; i++) totalPrice += Number(root.dgCart.getItemAt(i).item_price)
		root.txttotal.htmlText = "<b>Total:</b> "+ round2D(totalPrice);
		}

```

If anyone here could shed the light, I’d be sooooo greatful!
