ArgumentOutOfRangeException when toolstripmenuitem click
Hello, I'm trying to pass a variable from main form to child form. The code below is giving me error:
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.
VB.NET Code:
list = new List<string>();
DataGridViewRow row = programs_dgv.Rows[programs_dgv.CurrentRow.Index];
using (InsertData insertData = new InsertData())
{
list[0] = row.Cells[2].Value.ToString();
list[1] = row.Cells[3].Value.ToString();
list[2] = row.Cells[4].Value.ToString();
list[3] = row.Cells[5].Value.ToString();
list[4] = row.Cells[6].Value.ToString();
list[5] = row.Cells[7].Value.ToString();
list[6] = row.Cells[8].Value.ToString();
list[7] = row.Cells[9].Value.ToString();
list[8] = row.Cells[10].Value.ToString();
list[9] = row.Cells[11].Value.ToString();
list[10] = row.Cells[12].Value.ToString();
list[11] = row.Cells[13].Value.ToString();
list[12] = row.Cells[14].Value.ToString();
insertData._list = list;
insertData.ShowDialog();
list = insertData.GetValues();
if (insertData.cancelRequested == true)
{
return;
}
}
Re: ArgumentOutOfRangeException when toolstripmenuitem click
Firstly, does this line actually make sense?
csharp Code:
DataGridViewRow row = programs_dgv.Rows[programs_dgv.CurrentRow.Index];
Think about what that's doing. Why would you first get a row, then get the index of that row, then get the row at that index? Is it not obvious that you just end up with the same row that you started with?
As for your issue, have you made any effort at to determine the cause.
Quote:
Index was out of range.
Have you bothered to find out what the index was and what the range was? Do what you can for yourself first, then ask for help with what you can't.