DataRow OutOfRangeException...

G

Guest

Hi, I cannot understand why the following code gives me an
"OutOfRangeException" at deletion of the last datarow (i=9) to delete from
the datatable DT. Number of rows are 10 (max=10):

Dim max As Integer DT.Rows.Count
'************ USE ROW INFORMATION ****************
Dim message As String = ""
Dim i As Integer
Dim dr As DataRow
For i = 0 To (max - 1)
dr = DT.Rows(i) : message = message + CStr(dr(0)) + " " + CStr(dr(1))
+ ControlChars.CrLf
Next
'**************** DELETE ROWS *****************
For i = (max - 1) To 0 Step -1
dr = DT.Rows(i)
DT.Rows.Remove(dr)
Next
'********************************************

Have tried with Delete() & AcceptChanges, but with same error message.
 
T

Tim Wilson

What about using the Clear() method instead of looping through and removing
each row with the Remove() method?
 
G

Guest

I still get the OutOfRangeException if I use DT.Rows.Clear() or DT.Clear
instead. By the way, I must have the possibility to delete only part of the
row collection. The datatable is populating a DataGrid, but this should not
affect the datatable as such.

What makes me so frustrated is that I don't understand - as far as I can see
the DataTable should not show this behaviour.

MethMath
 
T

Tim Wilson

The following code works as expected for me.

Private DataTable1 As DataTable

....

DataTable1 = New DataTable("MyTable")
DataTable1.Columns.Add("Column1", Type.GetType("System.String"))
DataTable1.Columns.Add("Column2", Type.GetType("System.String"))

Dim i As Integer
For i = 0 To 9
DataTable1.Rows.Add(New Object() {"FirstName" & i.ToString(), "LastName" &
i.ToString()})
Next

Me.DataGrid1.DataSource = DataTable1

....

Dim i As Integer
Dim dr As DataRow
Dim max As Integer = DataTable1.Rows.Count
For i = (max - 1) To 0 Step -1
dr = DataTable1.Rows(i)
DataTable1.Rows.Remove(dr)
Next

This is pretty much the same row removal code that you posted. So if you are
running against the latest service pack (SP3)...
http://www.microsoft.com/downloads/...11-194b-4c00-b445-f92bec03032f&displaylang=en
.... then I would think that something else is causing this problem.
Alternatively, try the following code to see if it makes a difference.

While (DataTable1.Rows.Count > 0)
DataTable1.Rows.RemoveAt(0)
End While

If you are running the latest service pack and the above code still causes
the exception, maybe posting your complete DataTable and DataGrid usage code
will shed some better light on this.
 
G

Guest

I'm using SP3 and the code:

While (DataTable1.Rows.Count > 0)
DataTable1.Rows.RemoveAt(0)
End While

still gives me the same OutOfRangeException.

May it be a threading problem ? The DataGrid and DataTable are created in
main thread and the datatable-manipulating code that goes wrong is accessed
from another thread, although secured by SyncLock/End SyncLock.

Strangely, the error occurs only when number of datatable rows are larger
than 10 (exactly vertically filling the DataGrid - takes 10 rows without
scrolling).

MethMath
 
T

Tim Wilson

You might want to try dissociating the DataTable from the DataGrid, before
manipulating the rows, by setting the DataSource property of the DataGrid to
Nothing. Also, if you are currently attempting to remove rows on the
DataTable from another thread, try invoking (for example, using the Forms
Invoke() method) a delegate, to be run on the main thread, and actually
remove the rows through this delegate. UI updates need to be made from the
UI (main) thread.
 
I

Ilya Tumanov [MS]

You should never delete rows from underlying DataTable if it's bound to a
control via DataView.

Delete rows from DataView control is bound to instead.



Best regards,



Ilya



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Tim, you are of course correct ! "UI updates need to be made from the
UI (main) thread" - this is what I forgot when trying to produce code "en
masse".

I made a change so that the worker thread use a delegate for calling the
functions that updates UI in main thread - and now it works fine ! Now I have
to do the same for all other UI interaction from the various threads in my
application.

Thanks, Tim, for dragging me to the right track. It's really nice to have
you guys when reality screws up.

MethMath
 
T

Tim Wilson

Sometimes that's a difficult problem to diagnose since updating a UI element
from a non-UI thread can cause the app to hang, cause other undesired
behavior, or not cause a problem at all. Of course the chance should never
be taken and all UI updates should be passed back to the UI thread. So
whenever someone is having a problem and they mention that they're doing
something on a thread, the first question is usually "are you doing
something UI related on that thread?" Anyways, glad to hear that you're back
on track.
 

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