Skip to content

Data source based on custom business objects

Eddie Fisher

Asked Oct 21, 2006, 8:37 AM EDT3 answers
Share
LinkedInX
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.

  • 0
    In your Customer class, what does the public get property look like for City _LivesIn? It looks like you're just getting a .ToString() value.

    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
    0
  • 0
    It's hard to see from my first post, the LivesIn property of the Customer class is a reference to a City class. The get method for LivesIn returns just that, a reference to a City object. This is so I could write: tb1.text = Customer1.LivesIn.CityName; tb2.text = Customer1.LivesIn.ID;
    0
  • 0
    Hope this doesn't get posted twice - some glitch.

    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
    0