0

I'm relatively new to programming, so be easy!

An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

How Can i prevent these errors from occurring after iterating through the datagridview. The code does exactly what is needed. The for loop has iterated through all the rows but throws exception when no rows are left. How can i break out when all rows have been converted?

foreach (DataGridViewRow row in dataGridView1.Rows)
        {


            for (int i = 0; i < dataGridView1.Columns.Count; i++)
            {
                //String header = gridview_id.Columns[i].HeaderText;
               // String cellText = row.Cells[i].Text;

                partData.partID = Convert.ToString(dataGridView1.Rows[i].Cells[0].Value);
                partData.productName = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                partData.partDescription = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value);
                partData.unitPrice = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value);
                partData.quantity = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value);
                partData.partNote = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value);

                MessageBox.Show(PerformRequestUpdatePriority("http://dmcalla04.students.qub.ac.uk/import.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
                Console.WriteLine(username);


            }
        }

Many Thanks in advance.

2 Answers2

0

So basically you need to use the row variable instead of dataGridView1.Rows[i] The reason you're getting out of argument exception is in the cases that the number of rows are less than the number of columns you receive since dataGridView1.Rows[i] line tries to access index of an array that doesn't exist.

foreach (var row in dataGridView1.Rows)
{
    partData.partID = Convert.ToString(row.Cells[0].Value);
    partData.productName = Convert.ToString(row.Cells[1].Value);
    partData.partDescription = Convert.ToString(row.Cells[2].Value);
    partData.unitPrice = Convert.ToString(row.Cells[3].Value);
    partData.quantity = Convert.ToString(row.Cells[4].Value);
    partData.partNote = Convert.ToString(row.Cells[5].Value);

    MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
    Console.WriteLine(username);
}

or if you wanna use index, you can use following code:

for (int rowIndex = 0; rowIndex < dataGrid.Rows.Count; rowIndex++)
{
    var row = dataGridView1.Rows[rowIndex];
    partData.partID = Convert.ToString(row.Cells[0].Value);
    partData.productName = Convert.ToString(row.Cells[1].Value);
    partData.partDescription = Convert.ToString(row.Cells[2].Value);
    partData.unitPrice = Convert.ToString(row.Cells[3].Value);
    partData.quantity = Convert.ToString(row.Cells[4].Value);
    partData.partNote = Convert.ToString(row.Cells[5].Value);

    MessageBox.Show(PerformRequestUpdatePriority("http://dcosgrove04.students.cs.qub.ac.uk/importParts.php?", username, partData.partID, partData.productName, partData.partDescription, partData.unitPrice, partData.quantity, partData.partNote));
    Console.WriteLine(username);
}

You might also wanna check this answer

Community
  • 1
  • 1
manman
  • 4,743
  • 3
  • 30
  • 42
  • Hi, thank for your help. But this does not work due to row.Cells[]. It is underlined red stating 'object does not contain a defination for cells......' etc. Any other suggestions? – Dean Cosgrove May 06 '17 at 23:14
  • @DeanCosgrove based on [this link](https://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewrow.cells(v=vs.110).aspx) and also your code `DataGridViewRow` has a property called `Cells`. Can you confirm the case-sensitivity is correct. I also updated the code since you don't need the second loop as you've hardcoded the cell number – manman May 06 '17 at 23:19
  • Yeah i can confirm it still doesn't work :( The property cannot be called using this method. Thanks for the help man. – Dean Cosgrove May 06 '17 at 23:32
  • @DeanCosgrove no worries, I also added alternative for accessing the row as index which is more similar to your own original code. Can't help more without looking at your original code. Also need to know the exact error message in order to be able to help in fixing it – manman May 06 '17 at 23:39
  • Working, thank you very much!! Life saver. You shall be acknowledged in my dissertation... haha. Cheers man, appreciate it. – Dean Cosgrove May 06 '17 at 23:45
  • @DeanCosgrove haha no problem. – manman May 06 '17 at 23:46
0

Dean, When you try to reference a row or column in a DataGridView that does not exist, an 'out of range' error will result. In your case, your counter (i) repeats for the number of Columns, yet your code makes reference to the DGV's Rows. Fixing that error should solve your problem.

In response to your comment: Replacing the line:

for (int i = 0; i < dataGridView1.Columns.Count; i++)

with

for (int i = 0; i < dataGridView1.Rows.Count; i++)

Should prevent the error, but without knowing exactly what you're trying to accomplish, it's hard to guide you much further. You've got a nested loop in there and from what I can see, there's a lot in there that's redundant.

Jim Simson
  • 2,774
  • 3
  • 22
  • 30
  • Thank you, i understand what is happening in regards to the error. Its coding it to make it not happen i am stuck with. Any ideas? Dean – Dean Cosgrove May 06 '17 at 23:08
  • I've updated my answer in response to your question. – Jim Simson May 06 '17 at 23:19
  • Hi Jim, is your updated comment got the same for loops? I have taken out the redundant for loop. :) I would like the for loop to break/stop when the data grid rows has been iterated through to prevent the error from occurring. Eg. When all 3 rows have had the data extracted. Stop. Thank you for your assistance – Dean Cosgrove May 06 '17 at 23:36