Adding First Row to DataTable causes an error: System.IndexOutOfRangeException

B

Brandon

I have a very simple DataTable with two columns. If I remove all of
the rows in the datatable and attempt to add a new row, I receive the
following error:

A first chance exception of type 'System.IndexOutOfRangeException'
occurred in system.windows.forms.dll

Additional information: Index was outside the bounds of the array.


My code to add the new row is below. I am adding a new row for every
object in the array list MyArrayList:

'myDataTable is the dataTable in question

For i As Int16 = 0 To MyArrayList.Count - 1
Dim dr As DataRow = myDataTable.NewRow
dr.Item(0) = MyArrayList.Item(i)
myActivities.Rows.Add(dr)
Next

For some reason I am receiving the error when the ...Rows.Add(dr) is
called for the first row. If I close the program and restart it, I
don't receive the error. Again, this only happens if I first remove
all rows then attempt to add a new row.

I've heard that this may be a bug. Does anyone have any suggestions?

Thanks,

Brandon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top