wyrd
Senior Contributor
When setting the DataSource of a DataGrid to an ArrayList of objects, it automatically displays all properties of those classes. ie; If you had a Customer class that had two properties, Name and ID, they would be shown in the DataGrid.
My question is, how can I limit the properties that the DataGrid uses from its DataSource? Using the example above, say I only want to display the Name property from the Customer, and leave the ID out. The only solution I've been able to come up with is to turn the ID property into a method (ie; GetID()). Surely that's a valid solution, especially if the property is read-only anyway, but that solution may not always be applicable. If the Customer object grows into something more complex, and has the address and other info. available through properties, I can't just start redesigning all of my classes based on what DataGrid will and will not display.
However, another solution that presents itself is to create view classes which encapsulate the Customer object, CustomerView. Then only use that class for the DataGrid dispaly, and have the CustomerView class only provide properties which will be viewed in the DataGrid. But that isn't very flexible, and there may be situations where I want different customer data viewed at different times. Creating several different CustomerView classes just for this purpose doesn't sit well.
In any case, help would be appreciated. Thanks in advance.
EDIT:
Woops wrong forum, please move to Windows Forms. Thanks.
My question is, how can I limit the properties that the DataGrid uses from its DataSource? Using the example above, say I only want to display the Name property from the Customer, and leave the ID out. The only solution I've been able to come up with is to turn the ID property into a method (ie; GetID()). Surely that's a valid solution, especially if the property is read-only anyway, but that solution may not always be applicable. If the Customer object grows into something more complex, and has the address and other info. available through properties, I can't just start redesigning all of my classes based on what DataGrid will and will not display.
However, another solution that presents itself is to create view classes which encapsulate the Customer object, CustomerView. Then only use that class for the DataGrid dispaly, and have the CustomerView class only provide properties which will be viewed in the DataGrid. But that isn't very flexible, and there may be situations where I want different customer data viewed at different times. Creating several different CustomerView classes just for this purpose doesn't sit well.
In any case, help would be appreciated. Thanks in advance.
EDIT:
Woops wrong forum, please move to Windows Forms. Thanks.