0

I have a DataGridView, for which I need to loop through all of the columns and make certain columns visible / not visible, depending on the column name. I was using this :

With dgv
    For Each dtc As DataGridViewColumn In .Columns
        With dtc
            Select Case .Name
                Case "ColumnName1", "ColumnName2", "ColumnName3"
                    .Visible = True
                Case Else
                    .Visible = False
            End Select
        End With
    Next
End With

However, I've just noticed that, with this particular DataGridView, in the first iteration of the loop (which should set that particular column to not be visible), it throws an exception :

Collection was modified; enumeration operation may not execute

I found this question on SO (which is unrelated to DataGridView but reports the same type of exception) : For Each loop encountered 'Collection was modified; enumeration operation may not execute', but I need to change list's size at runtime

Which suggested (and I could be barking up the wrong tree here) that rather than iterating through the columns with a For / Each loop, I should use a List (Of T) instead (?)

Which I'm fine with (again, assuming this is actually the true root cause and appropriate solution) but I can't figure out how to take the DataGridViewColumnCollection for the grid (i.e. dgv.Columns) and assign it to a List such that I can iterate through that instead and see if that solves the issue?

Can anybody opine a) if what I'm trying to do is the correct approach and, if so b) how to generate the List from the column collection?

(What's really strange here is that I have another, very similar DataGridView, with a very similar routine - they are actually both different views of the same DataTable, just with different filters applied - and this exception never occurs with that one? And if I move off the control hosting the problem grid and then go back to it again, it goes through that same loop just fine without any exceptions? It's just the very first time it tries to perform the loop that the exception is thrown; subsequent attempts work fine? Which I find quite odd...)

Alan O'Brien
  • 151
  • 1
  • 13

1 Answers1

0

Posting this as "an" answer but not particularly fond of it as it seems clunky and requires looping through the collection twice (once to populate the list, then looping through the list itself) It does seem to prevent the problem from happening though (that exception isn't thrown any more) There must be a smarter / more efficient way of achieving this / getting round this problem...

Dim dgvColumns As New List(Of DataGridViewColumn)
For Each dtc As DataGridViewColumn In dgv.Columns
    dgvColumns.Add(dtc)
Next

For i As Integer = 0 To dgvColumns.Count - 1
    With dgvColumns(i)
        Select Case .Name
            Case "ColumnName1", "ColumnName2", "ColumnName3"
                .Visible = True
            Case Else
                .Visible = False
        End Select
    End With
Next
Alan O'Brien
  • 151
  • 1
  • 13