|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DataGridView - Property DataPropertyNameI 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 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.) 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; } } -- Show quoteHide quoteLife 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 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.)
Timespan perplexing, please help with brain freeze.
How does child class access parent's variables Reg expression Threading 101 Can the read() function in VB.NET start reading from somewhere else than 0 Frustrations with RTF format. VB proj file locations XML Logging Write one record Shared Variable Question VB.NET 2003 DirectoryInfo.GetFiles -> from all subfolders |
|||||||||||||||||||||||