Binding Dictionary values to a DataGridView

1k Views Asked by At

I had a List(Of MyClass) as the DataSource for a DataGridView control, and this worked nicely. A column was automatically created for each public property in MyClass.

I decided though that I wanted the List to be indexed by a string, so I changed it to Dictionary(Of String, MyClass) and of course the data binding no longer works.

I have tried ToArray(), ToList(), Values(), etc... but none of these return the data in a suitable structure to be displayed correctly in the DataGridView.

Is there a simple way to convert the ValueCollection from the Dictionary into some sort of enumerable list of MyClass? Or can anyone suggest a better data structure than Dictionary for this purpose?

Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

I have currently implemented this, by populating a new list from the dictionary for purposes of data binding. This is sufficient for my immediate needs, but is obviously inefficient.

        Dim ds As New List(Of MyClass)
        For Each v As Object In MyDictionary
            ds.Add(v.value)
        Next
        DataGridView1.DataSource = ds

I am sure there are better solutions, so would still welcome other answers.