469,573 Members | 1,222 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,573 developers. It's quick & easy.

DataGridView Binding with a Class

Hello

I have binding a class to a DataGridView and this class have a property from
another class and this i want binding to a DataGridViewComboBoxColumn. So
when i change a item in the Combo and leave this cell, then i have a error
(cannot convert String to cCountry).

I think the problem is the DataPropertyName in my
DataGridViewComboBoxColumn. Can anybody say me what is wrong?

Here my Code:

Public Class Form1

Private cClients As New System.Collections.Generic.List(Of cClient)
Private cCountries As New System.Collections.Generic.List(Of cCountry)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

With cCountries
.Add(New cCountry("CH", "Switzerland"))
.Add(New cCountry("D", "Germany"))
.Add(New cCountry("I", "Italy"))
.Add(New cCountry("F", "France"))
End With

With cClients
.Add(New cClient("Name1", "FirstName1", cCountries(0)))
.Add(New cClient("Name2", "FirstName2", cCountries(1)))
.Add(New cClient("Name3", "FirstName3", cCountries(2)))
.Add(New cClient("Name4", "FirstName4", cCountries(3)))
End With

DataGridView1.AutoGenerateColumns = False
DataGridView1.AutoSize = True
DataGridView1.DataSource = cClients

Dim column As DataGridViewColumn = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Name"
column.Name = "Name"
DataGridView1.Columns.Add(column)

column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "FirstName"
column.Name = "FirstName"
DataGridView1.Columns.Add(column)

dataGridView1.Columns.Add(CreateComboBox)

End Sub

Private Function CreateComboBox() As DataGridViewComboBoxColumn
Dim combo As New DataGridViewComboBoxColumn()
combo.DataSource = cCountries
combo.DataPropertyName = "Country"
combo.Name = "Country"
Return combo
End Function

End Class

Public Class cClient

Public Sub New(ByVal Name As String, ByVal FirstName As String, ByVal
Country As cCountry)
sName = Name
sFirstName = FirstName
oCountry = Country
End Sub

Private sName As String
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal Value As String)
sName = Value
End Set
End Property

Private sFirstName As String
Public Property FirstName() As String
Get
Return sFirstName
End Get
Set(ByVal Value As String)
sFirstName = Value
End Set
End Property

Private oCountry As cCountry
Public Property Country() As cCountry
Get
Return oCountry
End Get
Set(ByVal Value As cCountry)
oCountry = Value
End Set
End Property

End Class

Public Class cCountry

Public Sub New(ByVal Code As String, ByVal Title As String)
sCode = Code
sTitle = Title
End Sub

Private sCode As String
Public Property Code() As String
Get
Return sCode
End Get
Set(ByVal Value As String)
sCode = Value
End Set
End Property

Private sTitle As String
Public Property Title() As String
Get
Return sTitle
End Get
Set(ByVal Value As String)
sTitle = Value
End Set
End Property

Public Overrides Function ToString() As String
Return Me.Title
End Function

End Class
May 2 '06 #1
0 1612

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Nathan | last post: by
2 posts views Thread by michael sorens | last post: by
3 posts views Thread by =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.