Al Lelopath Al Lelopath - 5 months ago 77
Vb.net Question

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.

Answer

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
Comments