Al Lelopath
Al Lelopath

Reputation: 6778

How to convert a DataGridView to List

I've tried the following:

Compile error (on last As)

Dim myList As New List(Of Object)
myList = dataGridView.DataSource As List<Object> 

Throws Unable to cast object of type System.Windows.Forms.BindingSource:

myList = dataGridView.DataSource 

DataSource is a System.ComponentModel.BindingList<T> where T is a custom class.

Upvotes: 0

Views: 3157

Answers (2)

Al Lelopath
Al Lelopath

Reputation: 6778

This is how I did it:

Dim MyList As New List(Of Object) ' Of MyClass?
For Each myClass As MyClass In dataGridView.DataSource
    MyList.Add(myClass )
Next

Upvotes: 0

Jim Hewitt
Jim Hewitt

Reputation: 1792

You could do something like this

myList = dataGridView.Rows.OfType(Of Object).ToList()

Upvotes: 1

Related Questions