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