Data source based on custom business objects
I have a problem that's bugging me for a month now. I hope someone can help me solve it.
I'm trying to make a data source based on some classes and bind it to a BindingSource object.
The classes:
public class City
{
private Int32 _ID;
private string _Name;
...
}
public class Customer
{
private Int32 _ID;
private string _FirstName;
private string _LastName;
private City _LivesIn;
...
}
public class CustomerList : System.Collections.CollectionBase
{
...
}
Here's what I've been doing:
1. create a data source based on the Customer class and drag it on a form (creates a binding source)
2. on the form load event create an instance of the CustomerList class and load all the customers from the database
3. set the binding source's data source property to the CustomerList object
4. bind a data grid view to the binding source
The Problem:
In the data grid I can't display the name of the city for each customer. All I get is "MyNamespace.LivesIn". I tryed to create a new column with a lookup combo box, as with datasets, but it didn't work.
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Mike Hildner10 pointsPosted Oct 25, 2006, 4:18 PM EDT
You might want to have a public string City and return City.Name, or have a public int City and return City.ID.
If you did the latter it'd probably be easier to do the combo box thing.
Regards,
Mike
Eddie Fisher3 pointsPosted Oct 26, 2006, 5:35 AM EDT
Mike Hildner10 pointsPosted Oct 26, 2006, 7:54 AM EDT
Right, that's fine, but that's also why you're getting MyNamespace.LivesIn in the UI. You probably want to have another property named something like CityName that returns City.Name - that way you can show the city name in the UI.
If you want to do the combo box thing, make another property that gets/sets City.ID.
Regards,
Mike