[02/03] Collection was modified; enumeration operation may not execute.
Hi
I got 2 datagrid and single column. Datagrid 1 contains 2 rows number 2 and 3. Datagrid2 contains 1, 2 and 3. I want to remove the duplicate numbers on datagrid2. The tables are unrelated so i would have to use cursors.
VB Code:
Try
Dim a As String
For i As Integer = 0 To ds1.Tables(0).Rows.Count - 1
a = Me.DataGrid1.Item(i, 0).ToString.TrimEnd
For Each row As DataRow In ds2.Tables(0).Rows
''Console.WriteLine(row.Item(0).ToString)
If row.Item(0).ToString = a Then
ds2.Tables(0).Rows.Remove(row)
End If
Next
Next
Catch ex As Exception
Console.WriteLine(ex.Message)
End Try
Me.DataGrid2.DataSource = ds2.Tables(0)
I get the exception Collection was modified; enumeration operation may not execute. Any suggestions , thanks.
Jorge
Re: [02/03] Collection was modified; enumeration operation may not execute.
I would suggest you need to iterate through the rows backwards, as you are removing columns and that will change the boundries of your For Each Rows loop.
ie
Instead of
VB Code:
For Each row As DataRow In ds2.Tables(0).Rows
''Console.WriteLine(row.Item(0).ToString)
If row.Item(0).ToString = a Then
ds2.Tables(0).Rows.Remove(row)
End If
Next
Use
VB Code:
For intRow As Integer = ds2.Tables(0).Rows.Count - 1 to 0 Step - 1
Next