Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey!

So I'm trying to get the columns 'First Name' and 'Last Name' from my table 'contacts' in my Database into my combox, thouhg the way I'm trying to do it doesn't quite seem to work. My attempt looks like this currently, I'd appreciate any help

What I have tried:

private void comboBox_PartnerSelect_SelectedIndexChanged(object sender, EventArgs e)
{
    SimonTestDBEntities db = new SimonTestDBEntities();
    var contact = db.Contacts;

    foreach (var item in contact)
    {
        string a = Convert.ToString(item.Kunden_Vorname);
        string b = Convert.ToString(item.Name);
        string test = a + b;
        comboBox_PartnerSelect.Items.Add(test);

    }
}
Posted
Updated 13-Apr-18 1:40am
Comments
CS2011 13-Apr-18 6:27am    
have you tried setting the index after adding the items ?
F-ES Sitecore 13-Apr-18 6:37am    
"Doesn't work" doesn't give anyone enough information to help you. Would you phone a mechanic and say "My car doesn't work, how do I fix it?"

Detail what is happening that you don't want to happen, or what is not happening that you do what to happen.
Member 13777741 13-Apr-18 6:40am    
Sorry for the inconvenience!
It doesn't add the item to the combobox, thats what I was meant to say.
F-ES Sitecore 13-Apr-18 6:50am    
It looks like when you change the selected item in the comboBox_PartnerSelect combo you just add more items to it. Are you sure that's what you're intending to do? Use the debugger to step through the code to see why it isn't adding items. Check the event actually fires and then check that there are items in "contact".
BillWoodruff 13-Apr-18 15:25pm    
Modifying the contents of a ComboBox based on a selection in that ComboBox suggests a design problem. Usually, you would change the contents of a second ComboBox based on a selection change in a first ComboBox.

Declaring a new instance of a database in a ComboBox change event is also a possible code smell.

You can bind a ComboBox to a set of values using the DataSource property.

1 solution

Hey,so as far as I see you're using entity framework in order to do this.You're trying to add 2 columns into the combobox as far as I understand.Try this as see if it works:your m
private void comboBox_PartnerSelect_SelectedIndexChanged(object sender, EventArgs e)
{
using(SimonTestDBEntities db = new SimonTestDBEntities()){


var merge = Select(c => new { Kunden_Vorname = c.Kunden_Vorname, 
                                      Name=c.Name      
                                             DisplayValue = c.Kunden_Vorname + ": " + c.Name
                                           })
merge.ToList();
}

What I did here is I took the 2 columns and i passed the values from the database to the values that you have as a reference in your code.The DisplayValue is the DisplayMember here and the .ToList() method helps you display all the items that you have in the two columns.Hope it helps :)
 
Share this answer
 
v2
Comments
BillWoodruff 13-Apr-18 15:26pm    
Curious: what makes you think EF is used here ?
Daniel Andrei Popescu 17-Apr-18 7:27am    
When it has sth like "TstDemoDBEntities" it doesn't mean that he's using entity framework?If i'm mistaken,then I apologize :) This is a method that I have used when I had the binding with the combobox and it worked perfectly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900