473,549 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DataGridView - Property DataPropertyNam e

I have a datagridview that is bound to a cutom collection.
The classes contained in the custom collection have the properties that are
bound to the class . This works fine !
But if one of the properites exposes another object I would like to bind a
property of the sub object to the DataPropertyNam e this does not seem to
work.

eg main object customer
has a property called Address which is a contained object
inside object Address there is property call city.
so DataPropertyNam e = "Address.ci ty"
does not work !
Any ides

--
Life in the sun
Apr 7 '06 #1
3 8368
Hi

Thank you for your posting! My understanding on this issue is: When a
collection which contains some objects is bound to a DataGridView and the
object in the collection contains some sub objects, then how to show the
property of the sub object in the DataGridView. If I am off base, please
feel free to let me know.

When you specify a datasource to a DataGridView, some DataGridViewCol umns
according the datasource would be added to the DataGridView automatically.
Each DataGridViewCol umn added to the DataGridView has a DataPropertyNam e
which is set a property name of the object. When running, the DataGridView
will show the property value of the object. But if the property is an
object and you¡¯d like to show the property of the sub object, you could
reach the aim by overriding the sub object¡¯s ¡°ToString¡± method. Below is
a sample:

public class Customer

{

private Address _CustAddress;

public Customer()

{

this._CustAddre ss = new Address();

}

public Address CustAddress

{

get { return this._CustAddre ss;}

set { this._CustAddre ss = value;}

}

}

public class Address

{

private string _City;

public string City

{

get { return this._City; }

set { this._City = value; }

}

public Address()

{

this._City = "";

}

public override string ToString()

{

return this._City;

}

}

You need to set the DataPropertyNam e = "CustAddres s".

Please let me know if you have any other concerns, or need anything else.

Luke Zhang
Microsoft Online Community Support

=============== =============== =============== =====
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== =====

(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Apr 7 '06 #2
Luke thanks for the answer.
It sort of helps but say you have more than one property on the sub object
you want to access through the datapropertynam e for each column
eg.

col1.DataProper tyName = "Address.ci ty"
col2.DataProper tyName = "Address.postco de"

public class Address

{

private string _City;
private string _postcode

public string PostCode

{

get { return this._PostCode; }

set { this._PostCode = value; }

}

public string City

{

get { return this._City; }

set { this._City = value; }

}

public Address()

{

this._City = "";

}

public override string ToString()

{

return this._City;

}

}


--
Life in the sun
"WayDownUnd er" wrote:
I have a datagridview that is bound to a cutom collection.
The classes contained in the custom collection have the properties that are
bound to the class . This works fine !
But if one of the properites exposes another object I would like to bind a
property of the sub object to the DataPropertyNam e this does not seem to
work.

eg main object customer
has a property called Address which is a contained object
inside object Address there is property call city.
so DataPropertyNam e = "Address.ci ty"
does not work !
Any ides

--
Life in the sun

Apr 10 '06 #3
Hi,
Thanks for your reply. Unfortunately, there¡¯s no direct way to display a
owner object¡¯s properties and its sub-object¡¯s properties in one
DataGridView. But I suggest two methods to you may consider to get around
this problem. One method is to write some properties for the sub-object¡¯s
properties in the owner object. The following is an example.

public class Customer
{
private Address _CustAddress;
public Customer()
{
this._CustAddre ss = new Address();
}
public string CustAddress_Cit y
{
get { return this._CustAddre ss.City; }
}
public string CustAddress_Pos tCode
{
get { return this._CustAddre ss.PostCode; }
}

}
public class Address
{
private string _City;
private string _PostCode;
public string City
{
get { return this._City; }
set { this._City = value; }
}
public string PostCode
{
get { return this._PostCode; }
set { this._PostCode = value; }
}
public Address()
{
this._City = "";
this._PostCode = "";
}
}
You can set a DataGridView column¡¯s DataProperty Name as
¡°CustAddress_C ity¡± or ¡°CustAddress_P ostCode¡±.
And the other method is to merge your object collection into a datatable,
combine the properties of sub class as additional data columns and bind the
datatable to DataGridView.
Hope this is helpful to you.
Have a good day!

Sincerely,
Linda Liu
Microsoft Online Community Support
=============== =============== =============== ==========
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
=============== =============== =============== ==========

(This posting is provided "AS IS", with no warranties, and confers no
rights.)


Apr 10 '06 #4

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

Similar topics

2
9661
by: David Veeneman | last post by:
How can I set a bound DataGridView control to use a dataset table's column captions, instead of column names? I'm working with a DataGridView control, which I have bound to a table in a dataset. The dataset is created at run time, so I don't know column headers in advance. The table's column captions are set at run time, using the...
4
10057
by: Aaron Smith | last post by:
Ok, this is an odd one, but I could use some assistance with the framework 2 in VB.Net... I want to have a DataGridViewColumn, only have it use the ComboBox, then when they drop down the combobox, open up another datagridview instead of the combobox dropdownlist. The reason I want to do this, is so I can show more than one column at a time...
0
1802
by: Roger Odermatt | last post by:
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...
0
1280
by: Nathan | last post by:
Hi, I have a DataGridView that I'm binding to a List<> and then displaying specific properties. Where I have a problem is trying to display the property from an object which in itself is a property e.g. Contact object has a country object and I'm trying to display a property of the country object such as CountryName.
8
26395
by: | last post by:
I am sure this has been asked and answered, but here goes anyway... VS.Net 2005, VB.Net How can you display more than one field in the displaymember property of a combobox inside the datagridview control? I am at a loss. Thanks, David
3
94227
by: sklett | last post by:
I'm changing from a DataGrid to a DataGridView and have run across a problem. The items that are bound to the DataGrid have an int Property that represents a primary key of a lookup table in my database. For example: table JobTypes 1 | Manager 2 | Controller 3 | Supervisor table Employee
3
22823
by: connected | last post by:
I'm having difficulty with populating a DataGridView control with data correctly. It works with a single class, for example... class MyClass { private string _propertyOne; private string _propertyTwo; public string PropertyOne { get { return this._propertyOne; } }
2
10683
by: Stephen Costanzo | last post by:
I have: Public Class test Private displayValue As String Private dbType As Integer Property Display() As String Get Return displayValue End Get
4
40756
by: gregarican | last post by:
I have a standard VC# 2005 DataGridView showing a group of records coming from a table. Currently I have a CellMouseDoubleClick event popping up a MessageBox which pulls a timestamp of the transaction from a related table based on the row in the DataGridView that the double click was registered against. I didn't want to add the extra column...
0
7532
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7461
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...
0
7971
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...
1
7491
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5101
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1956
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1068
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
776
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...

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.