public void calculation() { int i; int checkExistance = 0; DataGridViewRow tempRow = new DataGridViewRow(); for (i = 0; i < balanceDgv.Rows.Count; i++) { tempRow = balanceDgv.Rows[i]; if ((string)tempRow.Cells[0].Value == buy_data.stockName) { checkExistance = 1; break; } } if (checkExistance == 0) { long totalCost = buy_data.buyCost * buy_data.buyQuantity; balanceDgv.Rows.Add(buy_data.stockName, buy_data.buyQuantity, buy_data.buyCost, buy_data.buyCost, 0, 0, totalCost, totalCost); } else if (checkExistance == 1) { long totalCost = (long)tempRow.Cells[7].Value + buy_data.buyCost * buy_data.buyQuantity; long valuationCost = (long)tempRow.Cells[6].Value + buy_data.buyCost * buy_data.buyQuantity; tempRow.Cells[1].Value = (long)tempRow.Cells[1].Value + buy_data.buyQuantity; tempRow.Cells[3].Value = totalCost / (long)tempRow.Cells[1].Value; tempRow.Cells[6].Value = valuationCost; tempRow.Cells[7].Value = totalCost; } long cash = userBalance.getCash(); cash -= buy_data.buyCost * buy_data.buyQuantity; userBalance.setCash(cash); long totalBuyCost = userBalance.getPurchaseAmount(); totalBuyCost += buy_data.buyCost * buy_data.buyQuantity; userBalance.setPurchaseAmount(totalBuyCost); }