DataGridViewColumn.remove - exception

M

Miro

I am getting an error removing columns from a datagridview.

I have a dataset created at runtime and a table with columns that are also
created at runtime.
The columns are based on other data.

When a button clicks, I want to reset the whole datagridview ( by removing
all the columns ) so I can add
a whole set of new columns to the list based on some parameters.

I can seem to delete the first column but the code gets an exception on the
'next' line.

For Each griddatacol As DataGridViewColumn In dgvTourneyLeaders.Columns
dgvTourneyLeaders.Columns.Remove(griddatacol.Name)
'dgvTourneyLeaders.Columns.Remove(griddatacol) 'same error
Next

System.InvalidOperationException was unhandled by user code
Message="Collection was modified; enumeration operation may not execute."


Thanks,

Miro
 
M

Miro

My Partial solution is that I create an arraylist with a list of "column
names" and I loop through the array list to remove the columns.

For Each ColName As String In WinnerColnames
dgvTourneyLeaders.Columns.Remove(ColName)
Next

Still wondering why the for next loop below doesnt work.
I know I am deleting from the list I am looping in - is it because I have
removed the column i was 'sitting on' in my list i was looping through?

Miro
 
C

Cor Ligthert[MVP]

Miro,

I assume that you databind your table to the datagridview, and that the
value of the defaultview of that (the name for the included dataview in a
datatable) is showed.

As you do that then work with the binded data, as that is the only data the
datagridview can than show.

Cor
 
J

Jack Jackson

I am getting an error removing columns from a datagridview.

I have a dataset created at runtime and a table with columns that are also
created at runtime.
The columns are based on other data.

When a button clicks, I want to reset the whole datagridview ( by removing
all the columns ) so I can add
a whole set of new columns to the list based on some parameters.

I can seem to delete the first column but the code gets an exception on the
'next' line.

For Each griddatacol As DataGridViewColumn In dgvTourneyLeaders.Columns
dgvTourneyLeaders.Columns.Remove(griddatacol.Name)
'dgvTourneyLeaders.Columns.Remove(griddatacol) 'same error
Next

System.InvalidOperationException was unhandled by user code
Message="Collection was modified; enumeration operation may not execute."

The exception is due to a restriction on Enumerators in .NET. In
general you can not modify a list while you are enumerating the list.

Change your code to something like one of these:

While dgvTourneyLeaders.Columns.Count >= 0
dgvTourneyLeaders.Columns.RemoveAt(0)

For indx As Integer = dgvTourneyLeaders.Columns.Count-1 To 0 Step -1
dgvTourneyLeaders.Columns.RemoveAt(indx)

For indx As Integer = dgvTourneyLeaders.Columns.Count-1 To 0 Step -1
Dim col As DataGridViewColumn =
dgvTourneyLeaders.Columns.Item(indx)

dgvTourneyLeaders.Columns.Remove(col)
 

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