473,396 Members | 1,891 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,396 software developers and data experts.

DataGridView - Property DataPropertyName

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 DataPropertyName 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 DataPropertyName = "Address.city"
does not work !
Any ides

--
Life in the sun
Apr 7 '06 #1
3 8348
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 DataGridViewColumns
according the datasource would be added to the DataGridView automatically.
Each DataGridViewColumn added to the DataGridView has a DataPropertyName
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._CustAddress = new Address();

}

public Address CustAddress

{

get { return this._CustAddress;}

set { this._CustAddress = 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 DataPropertyName = "CustAddress".

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 datapropertyname for each column
eg.

col1.DataPropertyName = "Address.city"
col2.DataPropertyName = "Address.postcode"

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
"WayDownUnder" 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 DataPropertyName 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 DataPropertyName = "Address.city"
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._CustAddress = new Address();
}
public string CustAddress_City
{
get { return this._CustAddress.City; }
}
public string CustAddress_PostCode
{
get { return this._CustAddress.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_City¡± or ¡°CustAddress_PostCode¡±.
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
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....
4
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...
0
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...
0
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...
8
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...
3
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...
3
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...
2
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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,...

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.