Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Data bind on Contained Object Properties

Data bind on Contained Object Properties

Scheduled Pinned Locked Moved C#
tutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • 8 Offline
    8 Offline
    8thWonder
    wrote on last edited by
    #1

    Hello :) I have an object, Car which contains an object, Engine as one of its members. I have a collection of Car objects and I want to bind a drop down list to the collection, displaying a property of the contained Engine object:

    class Car
    {
    public Engine engine; // contains the engine object
    public string manufacturer;
    }

    class Engine
    {
    public int capacity;
    }

    In my code I create a Cars collection: Cars cars = new Cars(); I add my car objects to the collection and bind my collection to a drop doown list:

    myDropDownList.DataSource = cars;
    myDropDownList.DataValueField = "engine.capacity";
    myDropDownList.DataTextField = "engine.capacity";
    myDropDownList.DataBind();

    This doesn't work though because the fields won't accept the syntax I use to specify the contained object's member. On the other hand, this works fine:

    myDropDownList.DataSource = cars;
    myDropDownList.DataValueField = "manufacturer";
    myDropDownList.DataTextField = "manufacturer";
    myDropDownList.DataBind();

    Can anyone tell me how to get my drop down list to bind so that I can display the contained object's member? Thanks :)

    J 1 Reply Last reply
    0
    • 8 8thWonder

      Hello :) I have an object, Car which contains an object, Engine as one of its members. I have a collection of Car objects and I want to bind a drop down list to the collection, displaying a property of the contained Engine object:

      class Car
      {
      public Engine engine; // contains the engine object
      public string manufacturer;
      }

      class Engine
      {
      public int capacity;
      }

      In my code I create a Cars collection: Cars cars = new Cars(); I add my car objects to the collection and bind my collection to a drop doown list:

      myDropDownList.DataSource = cars;
      myDropDownList.DataValueField = "engine.capacity";
      myDropDownList.DataTextField = "engine.capacity";
      myDropDownList.DataBind();

      This doesn't work though because the fields won't accept the syntax I use to specify the contained object's member. On the other hand, this works fine:

      myDropDownList.DataSource = cars;
      myDropDownList.DataValueField = "manufacturer";
      myDropDownList.DataTextField = "manufacturer";
      myDropDownList.DataBind();

      Can anyone tell me how to get my drop down list to bind so that I can display the contained object's member? Thanks :)

      J Offline
      J Offline
      John Petersen
      wrote on last edited by
      #2

      Hello Could you provide the code for the Collection class Cars? This would make it easier to help you.

      Kind Regards, John Petersen

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups