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. Binding combo-box to Entity Data Model

Binding combo-box to Entity Data Model

Scheduled Pinned Locked Moved C#
databasewpfwcfhelpquestion
3 Posts 2 Posters 1 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.
  • P Offline
    P Offline
    pmcm
    wrote on last edited by
    #1

    Hi, I'm trying to keep my data access code separate from the code behind my form. What I am trying to do is populate my combo-box with a list of CompanyNames here's how I'm trying to bind this combo-box to my Companies entity to retrieve the CompanyName:

    private void frmMain_Load(object sender, EventArgs e)
    {
    dal = new DataAccessLayer();

    cmbCompanyNameSearch.DataSource = dal.GetcmbCompanyList();
    

    }

    and here's the method in my DataAccessLayer:

    public List<Companies> GetcmbCompanyList()
    {
    // Check we have an ObjectContext
    if (entities == null) entities = new CompanySecretaryEntities();

        // Create a query from the entityset
    ObjectQuery<Companies> companies = entities.Companies;
    companies.MergeOption = MergeOption.AppendOnly;
    
    // Define the query
    var query = from c in entities.Companies
            select c.CompanyName;
    
    // Execute the query
    List<Companies> results = query.ToList();
    
    // Return the results in a List
    return results;
    

    }

    This is giving me the following error: Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<CompanySecretary.Companies Can anyone point me in the correct direction? I know if I change select c.CompanyName; to be select c; removes the error message but I only want to select c.CompanyName in this instance

    R 1 Reply Last reply
    0
    • P pmcm

      Hi, I'm trying to keep my data access code separate from the code behind my form. What I am trying to do is populate my combo-box with a list of CompanyNames here's how I'm trying to bind this combo-box to my Companies entity to retrieve the CompanyName:

      private void frmMain_Load(object sender, EventArgs e)
      {
      dal = new DataAccessLayer();

      cmbCompanyNameSearch.DataSource = dal.GetcmbCompanyList();
      

      }

      and here's the method in my DataAccessLayer:

      public List<Companies> GetcmbCompanyList()
      {
      // Check we have an ObjectContext
      if (entities == null) entities = new CompanySecretaryEntities();

          // Create a query from the entityset
      ObjectQuery<Companies> companies = entities.Companies;
      companies.MergeOption = MergeOption.AppendOnly;
      
      // Define the query
      var query = from c in entities.Companies
              select c.CompanyName;
      
      // Execute the query
      List<Companies> results = query.ToList();
      
      // Return the results in a List
      return results;
      

      }

      This is giving me the following error: Error 1 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<CompanySecretary.Companies Can anyone point me in the correct direction? I know if I change select c.CompanyName; to be select c; removes the error message but I only want to select c.CompanyName in this instance

      R Offline
      R Offline
      RobCroll
      wrote on last edited by
      #2

      select c.CompanyName

      is your problem. You are return a list of company names, not companies. Try

      select c

      Also the next problem you are going to have is with the ComboBox. Its only going to display the type of object "CompanySecretary.Companies". You'll need to check out the ComboBox's DisplayMember property to display the company name.

      "You get that on the big jobs."

      P 1 Reply Last reply
      0
      • R RobCroll

        select c.CompanyName

        is your problem. You are return a list of company names, not companies. Try

        select c

        Also the next problem you are going to have is with the ComboBox. Its only going to display the type of object "CompanySecretary.Companies". You'll need to check out the ComboBox's DisplayMember property to display the company name.

        "You get that on the big jobs."

        P Offline
        P Offline
        pmcm
        wrote on last edited by
        #3

        thank you for the reply, I have managed to get it working by changing the code behind my form to be:

        List companies = dal.GetCompanyList1();
        cmbCompanyNameSearch.DataSource = companies;
        cmbCompanyNameSearch.DisplayMember = "CompanyName";
        cmbCompanyNameSearch.ValueMember = "CompanyID";

        and renaming my DataAccessLayer method to GetCompanyList1

        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