Solved

Using a generic.dictionary as the source for a datagridview

Posted on 2007-03-29
2
2,279 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
ID: 18827564
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
ID: 18832699
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

18 Experts available now in Live!

Get 1:1 Help Now