Botsokoy91
Botsokoy91

Reputation: 5

datagridview get row value error "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index"

I just have this code that would get the value of the specific row in a datagridview. During the execution an exception message have been pop out. "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index".

Here is my sample code.

for (int i = 0; i < invoiceGridView.Columns.Count; i++)
        {
            MessageBox.Show(invoiceGridView[3, i].Value.ToString());
        }

Upvotes: 0

Views: 1411

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

You're iterating over invoiceGridView.Columns.Count, but then you're using i as the rowIndex parameter. The first parameter is the column index, not the second one:

dataGridView1[int columnIndex, int rowIndex]

Try this instead, which will display the value of each column in the 3rd row:

MessageBox.Show(invoiceGridView[i, 3].Value.ToString());

Upvotes: 1

Related Questions