0

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.

Phil Preen
  • 589
  • 5
  • 20
  • possible duplicate of [DataGridView bound to a Dictionary](http://stackoverflow.com/questions/854953/datagridview-bound-to-a-dictionary) – Junaith Dec 19 '14 at 12:37
  • @Junaith Thanks for the link. I had seen that question. It doesn't fully answer my issue though. In that case they were only using one property. The main answer is rather complex, and the code example is not well explained. I have tried some of the other suggestions, but have not been able to extend them to work with multiple properties. (Doesn't help that it is C# rather than VB, and my experience of Linq is limited). Also, it doesn't address the second part of my question, i.e. is there a more suitable data structure that I could use instead of a Dictionary? – Phil Preen Dec 19 '14 at 17:37

1 Answers1

0

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.

Phil Preen
  • 589
  • 5
  • 20