473,657 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 DataGridViewCom boBoxColumn. 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 DataPropertyNam e in my
DataGridViewCom boBoxColumn. Can anybody say me what is wrong?

Here my Code:

Public Class Form1

Private cClients As New System.Collecti ons.Generic.Lis t(Of cClient)
Private cCountries As New System.Collecti ons.Generic.Lis t(Of cCountry)

Private Sub Form1_Load(ByVa l sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load

With cCountries
.Add(New cCountry("CH", "Switzerlan d"))
.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.A utoGenerateColu mns = False
DataGridView1.A utoSize = True
DataGridView1.D ataSource = cClients

Dim column As DataGridViewCol umn = New DataGridViewTex tBoxColumn()
column.DataProp ertyName = "Name"
column.Name = "Name"
DataGridView1.C olumns.Add(colu mn)

column = New DataGridViewTex tBoxColumn()
column.DataProp ertyName = "FirstName"
column.Name = "FirstName"
DataGridView1.C olumns.Add(colu mn)

dataGridView1.C olumns.Add(Crea teComboBox)

End Sub

Private Function CreateComboBox( ) As DataGridViewCom boBoxColumn
Dim combo As New DataGridViewCom boBoxColumn()
combo.DataSourc e = cCountries
combo.DataPrope rtyName = "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 1810

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

Similar topics

3
7237
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 the applications. In ..NET 1.1, we favour to return DataTable from DAL or BLL and then press to datagrid's datasource and to bind data. Now, I think .NET 2.0 have some difference to bind data. Is there any good example let me to know how to...
1
18834
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 objects that is the datasource of a DataBinding that is bound to a DataGridView (WinForms) then I was expecting that any change to the data would be reflected in the DataGridView (in other words I was expecting the 2 way databinding to actually...
2
16335
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
11463
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 in datagridview2 that corresponded to a selected row in datagridview1. Great article/example.
2
9642
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 the columns are displayed. My DataSource objects are composed like this: class Customer : esCustomer // where esCustomer is the EntitySpaces class that was generated
2
7813
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: DataSet ds = new DataSet(); ds.ReadXml(@"\usr\tmp\sample.xml"); dataGridView.DataSource = ds; dataGridView.DataMember = "targetElement"; What I found through experimentation is that the DataMember may specify
5
7148
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 programmatically into the the underlying cell (i.e., I'm setting Value property of the cell based on user input, but the user is not directly entering any data). The problem:
3
6320
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 to get the data in a datagridview to change. For example 2 columns are ID and amount_paid, the datagridview loads on form load with all ID's and amounts. How do I get it to only bring back the selected ID. Sounds like I may need to change the SQL...
11
76166
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 else who got stumped like I did. It seems so simple... binding a DataGridView to a List<T>. These are the two general problems that I kept running into: (1) When the data in the list updated, the data on the screen did not update. (2) When I...
0
8316
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8737
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8610
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7345
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6174
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4327
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1967
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1730
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.