Collection Was Modified; Enumeration Operation May Not Execu

May 22 2020 1:49 PM
Hello everyone,
 
I am new in VB.net, I have a treeview which first level node is populated with years. I have written some code which should read each node from tree view and add its months from datatabe under the year. I have written two nested For Each loop in vb.net but face the below problem. Please help me to solve the issue.
 
Error:
Collection Was Modified; Enumeration Operation May Not Execute.
 
Code:
 
Dim monthdt As DataTable = getMonthData()
For Each yearNode As TreeNode In TreeView2.Nodes
    For Each row As DataRow In monthdt.Rows
         Dim month As New TreeNode()
         If row("year").ToString() = yearNode.Text Then
               month.Text = row("month").ToString()
               month.SelectAction = TreeNodeSelectAction.Select
              TreeView2.Nodes.Add(month)
        End If
Next
Next
 

Brought to you by:

Answers (1)