i need help. I tried to solve an equation through the records in the table, so i do not know how to use the values in each rows on the table.
Table Image: https://i.stack.imgur.com/fZuVb.jpg
So I need to calculate the avg, when the item is y, ( balance = previous balance - y's qty ) , ( price = X's Average, Avg= X's Avg).
While the item is X, ( balance = previous balance + qty) , ( Avg = (previous balance * previous Avg) + (price * qty) / balance.
`
foreach (DataRow row in ds.Tables[0].Rows)
{
//row[6]=qty
//row[7]=price
//row[8]=balance
//row[9]=avg
int ybalance = Convert.ToInt32(row[8]) - Convert.ToInt32(row[6]);
int xbalance = Convert.ToInt32(row[8]) + Convert.ToInt32(row[6]);
double TotalCost = Convert.ToInt16(row[6]) * Convert.ToDouble(row[7]);
double avgcost = Convert.ToDouble(row[9]);
double avg = ((ybalance * avgcost) + TotalCost) / xbalance;
if (Convert.ToInt32(row[7]) == null)
{
row[8] = ybalance;
row[7] = Convert.ToDouble(row[9]);
row[9] = Convert.ToDouble(row[9]);
}
else
{
row[8] = xbalance;
row[9] = avg;
}
` Any hints or suggestions?
Thanks :)