Solved

Using a generic.dictionary as the source for a datagridview

Posted on 2007-03-29
2
2,260 Views
Last Modified: 2011-10-03
vs2005 vb.net
Public Class test

Private displayValue As String
Private dbType As Integer
Property Display() As String
      Get
            Return displayValue
      End Get
      Set(ByVal value As String)
            displayValue = value
      End Set
End Property

Property DataType() As Integer
      Get
            Return dbType
      End Get
      Set(ByVal value As Integer)
            dbType = value
      End Set
End Property

Public Sub New(ByVal dspl As String, ByVal dt As Integer)
      Display = dspl
      DataType = dt
End Sub
End Class
I have a generic.dictionary [Dim tests As New Generic.Dictionary(Of String, test)] which i would like to bind to a datagridview

if it were a collection (which contained a series of class 'test'), I would :
      datagridview1.datasource = collName
      datagridview1.columns(0).datapropertyName = "Display"
      datagridview1.columns(1).datapropertyName = "DataType"

However I don't appear to be able to do the same thing with a dictionary. So far I found that you need to do:
      datagridview1.datasource = tests.values

But I do not know how to bind the specific value of the class for the dictionary.

Thank you
0
Comment
Question by:syscomdev
2 Comments
 
LVL 41

Accepted Solution

by:
graye earned 500 total points
Comment Utility
You are correct... the Generic.Dictionary class does not support the IList (or any of the other supported interfaces) that would qualify it for being a DataSource.

I'd recommend you use a BindingList instead of a Dictionary.   There is a pretty good example that is almost an exact match to your example at the MSDN web site at:

http://msdn2.microsoft.com/en-us/library/ms132679.aspx
0
 

Author Comment

by:syscomdev
Comment Utility
The one thing that I would lose would be the ability to find a value by a key, i.e.
tests("example1").datatype = 5.

The item method in the BindingList is only by numeric.
So is my basic choice which is more important? To be able to gather the data by key or to have it displayed?

I know I could have a simple data construct for the pairing, but was hoping to get around it.

Thank you for the example and any other assistance.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now