views:

39

answers:

1

Okay I have a problem with my Windows Application. My DataGridView consists of the following columns: ProductName, Qty, Price, Subtotal. So I am assuming that the Cell Indexes of these are respectively as follows: 0, 1, 2, 3. However, whenever I try executing the code below:

txtSubtotalProducts.Text = "Php " + (Convert.ToDouble(dgvProducts.Rows[0].Cells[3].Value) + Convert.ToDouble(dgvExpenses.Rows[1].Cells[3].Value) + Convert.ToDouble(dgvExpenses.Rows[2].Cells[3].Value)).ToString();

However, whenever I run this code block, I bump into this error message: Index Out of Range.

Am I missing anything in here? Thanks alot in advance.

EDITED: Sorry, index 4 was actually something I tried, but originally, I am trying both 2 and 3 as indexes without any luck. However, when I try 1 (which is the Qty column) it works perfectly.

A: 

Stephan is correct! If you have ProductName, Qty, Price, Subtotal that is 4 columns.

So as the arrays are 0 based, it the possible columns you can access is 0,1,2 and 3.

So when you try to access

dgvExpenses.Rows[1].Cells[4].Value

As there is no 5th column (4th index), it will throw a Index Out of Range exception :)

Ranhiru Cooray
I was soo stupid. The error I am bumping into was caused by the fact that I was accessing the wrong grid view. I should be accessing the dgvProducts but i was accessing the Expenses as well which only has 2 columns. Thanks guys. :D
Kim Rivera