DataGridView and Generic.Dictionary (VB.NET 2005)

S

Stephen Costanzo

I have:

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

On the form:
Dim tests As New Generic.Dictionary(Of String, test)
{ Set the values into tests }
DataGridView1.DataSource = tests.Values
( Thought DataGridView1.Columns(0).DataPropertyName = "Display" would work
but it does not )

I have a datagridview on my form that i want to bind to this dictionary.
When using a collection I was able to do this without a problem by
specifying the properties as the DataPropertyName, however this does not
seem to work when I use the Dictionary. The Dictionary provides other
necessary benefits, which is why I switched to it from a collection.

Is there a different control I should use to display the data in a grid? Or
do I need to wrap tests into something else for the purposes of displaying
the data?

Thank you
 
M

mnadeem.abbasi

I have:

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

On the form:
Dim tests As New Generic.Dictionary(Of String, test)
{ Set the values into tests }
DataGridView1.DataSource = tests.Values
( Thought DataGridView1.Columns(0).DataPropertyName = "Display" would work
but it does not )

I have a datagridview on my form that i want to bind to this dictionary.
When using a collection I was able to do this without a problem by
specifying the properties as the DataPropertyName, however this does not
seem to work when I use the Dictionary. The Dictionary provides other
necessary benefits, which is why I switched to it from a collection.

Is there a different control I should use to display the data in a grid? Or
do I need to wrap tests into something else for the purposes of displaying
the data?

Thank you

Dear Stephen Costanzo

I have make some alternation in your code
that is

---
Dim col As New DataGridViewTextBoxColumn

Dim tests As New Generic.Dictionary(Of String, test)
{ Set the values into tests }
with DataGridView1
..DataSource = tests.Values
col.HeaderText = "Display"
col.DataPropertyName = "Display"
.Columns.Add(col)
end with

-- check these lines of code in keeping your code scenario
feel free to have any sort of problem regarding development

I am here at mna_4u(at)hotmail.com
or mnadeem.abbasi(at)gmail.com
 
S

Stephen Costanzo

I implemented what you gave me as such:

Dim tests As New Generic.Dictionary(Of String, test)
Dim col0 As New DataGridViewTextBoxColumn
Dim col1 As New DataGridViewTextBoxColumn

tests.Add("Steve", New test("Steve", 2))
tests.Add("Matt", New test("Matt", 3))

col0.HeaderText = "Display H"
col0.DataPropertyName = "Display"
col1.HeaderText = "TypeH"
col1.DataPropertyName = "DataType"

DataGridView1.DataSource = tests.Values
DataGridView1.Columns.Add(col0)
DataGridView1.Columns.Add(col1)

My grid only shows the headers without any information. I open the
DataGridView1.DataSource in the watch window and I see the Items collection
with two entries. However it does not appear to be appearing in the grid.
Am I missing something else?

Thank you for your help so far
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top