Set Combobox Datacource using Entity Data Model

ssabc

Well-known member
Joined
Dec 11, 2017
Messages
63
Programming Experience
10+
Hello:

I would like to simply set the datasource for my combo box to look at the Jobs (table). Customer (field).



C#:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]private [/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] loadJobs()[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
        {

            
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]using[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] db = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ResourcePlanningEntities())[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]
            {

                dgvJobs.DataSource = db.Jobs.ToList();

                cboCustomer.DataSource = db.Jobs.All(a => a.Customer == 
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515][FONT=Consolas][SIZE=2][COLOR=#a31515]"*"[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]);[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]

?

            }

           

        }
[/SIZE][/FONT][/SIZE][/FONT]



Something is a little off.

Thanks!
 
Set the DisplayMember property to the name of the property you want displayed in the ComboBox, then assign the list to the DataSource property. I shudder to think of how many examples of that there must be on the web.
 
Looking at the code you have, you're trying to create a new list that contains just those values. You can do that but you should generally not. Assuming that you were going to go that way though, let's look at what's wrong with your code.

Firstly, there's no place for the All method there. That All method is intended to test whether all items in a list satisfy a condition, e.g. all CheckBoxes are checked. It returns a single Boolean value and that's obviously not what you want. If you're trying to get a value out of each item then you would use the Select method. If you want to filter the items then you would use the Where method.

You also need to call ToList or ToArray on the result. WinForms data-binding requires an IList instance while LINQ methods like Select and Where return an IEnumerable. ToList and ToArray will produce a List(Of T) and an array respectively, both of which implement IList.
 
Queries using Entity Data Framework??

Thanks for your reply.

I was able to do this:
C#:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]cboCustomer.DataSource = db.Jobs.Select(a => a.Customer).Distinct().ToList();
[/SIZE][/FONT][/SIZE][/FONT]


Can you tell me if anything like this (but of course not this) is possible?

C#:
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]cboCity.DataSource = db.Jobs.Select(a => a.City).Where(b => b.Customer == cboCustomer.ValueMember).Distinct().ToList();[/SIZE][/FONT][/SIZE][/FONT]

I want to base other fields such as customer and state off of customer names, and not all of the state names. I've been looking online. You said these things were everywhere on line, however one would have to know what to look for, right? If you have a good book you'd recommend, I would gladly buy it.

Thanks again.
 
Last edited:
You said these things were everywhere on line, however one would have to know what to look for, right?

I said that there was plenty of information about binding a ComboBox online. This is really nothing to do with binding a ComboBox. That is a big part of your problem. Yes, you need to know what to look for, so you need to actually think about what you're doing. As you can see, you're still setting the DataSource of the ComboBox so the part that actually relates to the ComboBox has not changed one single bit. What you're asking about here is filtering data, which has exactly zero to do with ComboBox data-binding.

You start with a list of Jobs. From those Jobs you select the City of each. You now have a list of something that is not Jobs. What is Customer? Is it a property of a Job? Wouldn't it make sense to filter a list of Jobs by a property of a Job?

Consider this. Let's say that I was in a room full of people. Let's say that we wanted a list of the names of the people who were wearing red shirts. Let's say that I was to write down the name of each person on a piece of paper and then take that piece of paper to you in another room and ask you to pick out the names of the people wearing red shirts. Could you do it? Of course you couldn't, because all you have is names and you can't see the shirts. I would have needed to look at the shirts first and then only take down the names of those people who had a red shirt, right? So, what do you think you need to do in your case?
 
Filtering Questions

Hello:

I realize I have no idea what I am doing in c# with Entity Framework. However, I would like my combo box values to reference like items in my table, in this case Jobs.

In the code below, the highlight is obviously wrong.


C#:
[FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] vCity = db.Jobs.Select(a => a.City).Where(b => [COLOR=#ff0000][B]b.[/B][/COLOR]Customer == vCustomer).Distinct.ToList();[/SIZE][/FONT][/SIZE][/FONT]

Can you help me with the correct syntax? I've been reading blips all day online about filtering. Happy for a good link as well. The problem is, many articles assume certain things are known. It must be that I cannot combine Selects with Wheres. Also, can the alias names be reused, or should they always be different. I am still trying to figure out how to just call these things as objects. In my opinion, I have the database table as an object, but not its fields. How is this normally handled?


C#:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] loadJobs()[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]{
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]    using[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] db = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ResourcePlanningEntities())[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]    {
       dgvJobs.DataSource = db.Jobs.ToList();
       
       [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] vCustomer = db.Jobs.Select(a => a.Customer).Distinct().ToList();[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]        cboCustomer.DataSource = vCustomer;
       
       [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] vCity = db.Jobs.Select(a => a.City).Where(b => [COLOR=#ff0000][B]b.[/B][/COLOR]Customer == vCustomer).Distinct.ToList();[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]        cboCity.DataSource = vCustomer;

   }
 
}
[/SIZE][/FONT][/SIZE][/FONT]

In this example, I have instantiated j to be the Jobs table located under Model1.tt. The problem is, the value is NULL.

C#:
[FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]private[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] loadJobs()[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]{            
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]    using[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ([/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] db = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] ResourcePlanningEntities())[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]    {
        [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] j = [/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] Job();[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]        dgvJobs.DataSource = db.Jobs.ToList();
[/SIZE][/FONT][/SIZE][/FONT][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff][FONT=Consolas][SIZE=2][COLOR=#0000ff]        var[/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][/COLOR][/SIZE][/FONT][FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2] vCustomer = j.Customer;[/SIZE][/FONT][/SIZE][/FONT]
[FONT=Consolas][SIZE=2][FONT=Consolas][SIZE=2]        cboCustomer.DataSource = vCustomer;
[/SIZE][/FONT][/SIZE][/FONT]
 
Last edited:
It's like you didn't even read my previous post.
You start with a list of Jobs. From those Jobs you select the City of each. You now have a list of something that is not Jobs. What is Customer? Is it a property of a Job? Wouldn't it make sense to filter a list of Jobs by a property of a Job?
Where are you filtering a list of Jobs in that code?
 
I read the post.

Jobs is the table, which contains properties like customer, city, state. and so forth. Si the fields are then, properties.

What I am looking for is the syntax to treat these items like objects.
 
What you should be looking for is an understanding of what the methods you're calling actually do. As I have said several times, you need to filter first. It's the Where method that does the filtering so you need to call Where first. You are currently calling Select and getting the City of each Job. You're then trying to filter those City values on Customer but a City value doesn't have a Customer property so that is nonsensical. The thing that has a Customer is a Job so if you want to filter on Customer then you haver to filter a list of Jobs. Filter the Jobs on Customer first, then get the City from each matching Job.

To be frank, this is just common sense. If I asked you to do as I said earlier - i.e. get the names of people wearing red shirts - I've no doubt that you could do it. You'd be able to perform the steps involved, so why would you then reverse the order of the steps because you're doing it in code? This is what comes from writing code without a clear idea of what it's supposed to do first. I don't mean the results that it's supposed to produce but rather the steps it's supposed to perform to produce those results. You don't need any programming experience at all to come up with those steps so inexperience is no impediment. Once you have the steps written down, you have something to compare your code to to see whether it does as intended. If your code does two things in the reverse order to your algorithm then the issue is obvious. If you have no algorithm then how would you know? A large proportion of beginner mistakes are the result of a failure to plan what code is supposed to do before writing it. You're not the only one who does this. You can now choose to do something about it or not.
 
In my world, compared to your example of getting the names of the people wearing red shirts, I am looking to get the cities for a customer. Having spent years doing ADO in VB with recordsets and select statements (often used as a DataSource for a control, I am trying to look at this from the perspective of using C#, which I believe better lends itself to tables and fields being actual objects. Or, is the field a method to the object?

So again, what is the syntax to do this filtering, because every example I have researched is not producing this result?

This works, but only for the top line. If I add another line for another field, I get nothing.

C#:
var vCustomer = db.Jobs.Select(a => a.Customer).Distinct().ToList(); works!

var vCity = db.Jobs.Select(b => b.City).Distinct().ToList(); fails?
I would like to do this (below), but it produces a null value because I do not know how to filter it.



C#:
var j = new Job();
 
 
var vCity = j.City.Distinct().ToList();
 
Last edited:
In my world, compared to your example of getting the names of the people wearing red shirts, I am looking to get the cities for a customer.
It appears that you don't understand the concept of an example. In my example, I started with a list of people, filtered them by shirt colour and then got the name of each matching person. In your case, you are starting with a list of Jobs and you need to filter them by Customer and then get the City for each matching Job. If you cannot see how the example can be applied to your situation then you have a significant issue before writing a single line of code. A huge part of programming is gaining an understanding of a concept from an example and then applying that concept to your own situation. The concept demonstrated by my example is directly applicable to your current situation.

In my previous post, I was even more explicit and made this statement:
It's the Where method that does the filtering so you need to call Where first.
In your latest code, I don't even see a call to Where. If you're not going to follow instructions when they are provided then why are we even here?

As for the last part of your post:
I would like to do this (below), but it produces a null value because I do not know how to filter it.
C#:
var j = new Job();
 
 
var vCity = j.City.Distinct().ToList();
What does that even mean? How about you actually explain what you're trying to achieve instead of expecting us to guess that from code that doesn't actually achieve it? The code doesn't make sense as it is so how are we supposed to know what it's supposed to do. Taking the code at face value, it initially creates a new Job object. Like any other new object, that Job will be essentially "empty", i.e. all it's fields will be their default values, which generally mean null for reference types, e.g. Strings, and zero for numbers. That means that, unless you have code inside the Job class to make it otherwise, the City property is going to be null. Even if it wasn't null though, how would calling Distinct on it make sense? The point of the Distinct method is to remove duplicates from a list. How is the City property a list? If it's a String then what will actually happen is that it will be treated as a list of characters, which is obviously not useful.

I think that a big part of your issue is not forming a clear picture of what you're trying to achieve in your head first. If your idea of what you're trying to do (I'm talking about the steps to achieve a result, not the result itself) is vague then the chance of you implementing what you need is very low. One way this problem manifests is a failure to provide a FULL and CLEAR explanation of what you're trying to achieve to us. It also manifests is the fairly obvious issue that I have pointed out numerous times, i.e. you're filtering at the wrong stage of the process. If you simply speak it out loud or write it down then the code to implement it becomes much plainer. You want to get all the Jobs for a particular Customer (that's the filter, i.e. the Where call) and then you want to get the City for each of those matching Jobs (that's the projection, i.e. the Select call) and then you want to remove duplicates (that's the Distinct call) and then you want to put that into am IList so that it can be bound (that's the ToList call). Your issue is not syntax. It's performing the steps you need in the correct order, as I pointed out some time ago.
 
I think it is a fair assessment to say that this post is exhausted. I have not seen one code example from you on this post, yet you say "In my last example." People with red shirts, or cities in companies, whatever.

I thought the whole point of Entity Framework was to create objects of fields and point to them.

Conceptually, the principle is easily produced with old school ADO.NET. cboCity.DataSource = "SELECT City FROM tblCustomer WHERE Customer = "ABC Corporation"

I am fine if you are not interested in answering my question, we can just be done with this post.

Have a great weekend, and thank you for whatever you were trying to help me understand.
 
Back
Top Bottom