0

I want to calculate the Total Price and display it in the Data Grid View. The fourth column does not retrieve information from the database, so I don't know how to do this. Here's what the GUI looks like:

enter image description here

Here's the code I have so far...

                double colTotal = 0; //set column total to 0

                foreach (DataRow currentRow in itemDS.Tables[0].Rows)
                {
                    // add to data grid view
                    invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString());

                    //Decalare variables for item quantity and price
                    var itemQuant = Convert.ToDouble(currentRow[1]);
                    var priceEach = Convert.ToDouble(currentRow[3]);

                    //Multiply the item quantity by the item price
                    double subTotal = itemQuant * priceEach;

                    //do this for each row
                    colTotal += subTotal;
                }

                //Display subtotal
                subTotalOutput.Text = colTotal.ToString();
                //Calculate tax
                double tax = colTotal * .073;
                //Display total tax
                taxOutput.Text = Convert.ToString(tax);
                //Add the subtotal and the total tax
                double totPrice = colTotal + tax;
                //Display total price
                totalOutput.Text = Convert.ToString(totPrice);

            }

2 Answers 2

1

I believe you can just do the subTotal calculation before you populate the grid, and add the subTotal variable on the end to your call to dataGrid.Rows.Add(). Like so:

foreach (DataRow currentRow in itemDS.Tables[0].Rows)
{
    //Decalare variables for item quantity and price
    var itemQuant = Convert.ToDouble(currentRow[1]);
    var priceEach = Convert.ToDouble(currentRow[3]);

    //Multiply the item quantity by the item price
    double subTotal = itemQuant * priceEach;


    // add to data grid view
    invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString(), subTotal);

    //do this for each row
    colTotal += subTotal;
}
Sign up to request clarification or add additional context in comments.

Comments

0

I'll start by saying that you could make your life easier using the DataSource property of the DataGridView control, but to resolve your issue given your current setup, I would simply move some code around and do it like this:

double subTotal = itemQuant * priceEach;

invoiceDataGridView.Rows.Add(currentRow[1].ToString(), currentRow[2].ToString(), currentRow[3].ToString(), subTotal.ToString());

1 Comment

Thank you so much. I wasn't sure where to add this at first, but I got it figured out. Thank you :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.