I am trying to add 2 decimal values and save it to a new datacolumn. But i realize its not how i should do it. Where am i thinking wrong, Please let me know. Thank you! This is in Visual Studio 2005 using c#..
Dataset ds = new DataSet();
DataColumn newColumn2;
newColumn2 = new DataColumn("TotalAmount");
decimal TotalAmountSold = 0;
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
TotalAmountSold = Convert.ToDecimal(ds.Tables[0].Rows[i]["AmountSold"]) + Convert.ToDecimal(ds.Tables[0].Rows[i]["AmountUpgraded"]);
}
TotalAmountSold = Math.Round(Convert.ToDecimal(TotalAmountSold.ToString()), 2);
newColumn2.Expression = TotalAmountSold.ToString();
ds.Tables[0].Columns.Add(newColumn2);
Take a look at the examples on MSDN. You basically want to create a column, specify the formula and then add that column to a DataTable. You’ll end up with something like
You don’t need to manually loop through all of the rows, that will be computed for you.