473,379 Members | 1,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 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 1789

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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.