472,354 Members | 2,150 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

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 1722

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: abc my vclass | last post by:
There are some programs written on .NET 1.1. These applications are apply n-tiers contains Data Access Layers or Business Logic Layer. Now, our company upgrade to .NET 2.0 and enhance or rewrite...
1
by: Dave A | last post by:
Hi, I am struggling with two way databinding in WinForms and the DataGridView. I am binding to business object classes (rather than datatables). If I have a collection of these business...
2
by: Nathan | last post by:
Hi, I have a datagridview bound to a List of objects (ObjectA). Each ObjectA contains an ObjectB property. Class ObjectA { public ObjectB objB {} }
2
by: Rich | last post by:
Hello, Following an example at http://www.vb-tips.com/dbpages.aspx?IA=DG (by Cor Lightert and Ken Tucker) on binding a dataRelation to a Datagridview for sqlClient, I was able to view rows...
2
by: Steve | last post by:
Hi- OK, I've got a DataGridView, I've created a BindingSource from one of my Business Entity object (based on generated classes from EntitySpaces) I've left the default column setup so that all...
2
by: michael sorens | last post by:
I have been trying to figure out how to use DataSets, BindingSources, DataGridViews, and XML together, but it is a challenge. I understand how to populate a DataGridView with XML basically as: ...
5
by: DanThMan | last post by:
The situation: * I have a ButtonColumn in a DataGridView. * When the user preses one of the buttons, a dialog appears. * Based on what the user selects in the dialog, data is entered...
3
by: =?Utf-8?B?Sm9obiBCdW5keQ==?= | last post by:
New to databinding in vs2005, I always did it manually in 2003. I have no problem loading comboboxes, and a change in that combobox changes the data in the textboxes but I can not figure out a way...
11
by: dave18 | last post by:
Hello all! I found a solution to my original question, but there's still so much I don't understand about it, I thought I'd give this forum a try. At the very least, maybe it will help someone...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.