Reputation: 43
I have made a shop app for a school project it adds goods from goods tab to factor tab everything is working fine but I don't know how to calculate all - total price rows to make a variable that holds the price you have to pay EDIT: now I'm getting the error in the picture
int n = dgvfactor.Rows.Count - 1;
dgvfactor.Rows.Add();
dgvfactor.Rows[n].Cells[0].Value = txtFGoodsCode.Text;
dgvfactor.Rows[n].Cells[1].Value = lblgoodsname.Text;
dgvfactor.Rows[n].Cells[2].Value = txtFAmount.Text;
dgvfactor.Rows[n].Cells[3].Value = lblprice.Text;
int amount = int.Parse(txtFAmount.Text);
int price = int.Parse(lblprice.Text);
int total = (amount * price);
lbltotal.Text = total.ToString();
dgvfactor.Rows[n].Cells[4].Value = lbltotal.Text;
var totalPrice = 0;
int rowPrice;
for (int i = 0; i < dgvfactor.Rows.Count; i++)
{
if (int.TryParse(dgvfactor.Rows[i].Cells[4].Value.ToString(), out rowPrice))
{
totalPrice += rowPrice;
}
}
lblpricetopay.Text = totalPrice.ToString();
Upvotes: 3
Views: 91
Reputation: 1322
Sorry If im mistaken here, but can't you just simply use a for-loop ?
var totalPrice = 0;
int rowPrice;
for (int i = 0; i < dgvfactor.Rows.Count; i++) {
// Note the NOT (!) new row...
if (!dgvfactor.Rows[i].IsNewRow) {
if (int.TryParse(dgvfactor.Rows[i].Cells[4].Value.ToString(), out rowPrice))
{
totalPrice += rowPrice;
}
}
}
Upvotes: 4