Marc Karam
Marc Karam

Reputation: 465

ComboBox formatting

I know this is probably a stupidly easy question to answer but I'm going to ask it anyways.

So I have a combobox that when it's got data in it looks like this

enter image description here

I want to know if there is any way to make the data look like this instead while still using only 1 combobox

John, Doe

Nickel, Back

etc...

Any help would be appreciated.

Upvotes: 0

Views: 85

Answers (1)

abdul
abdul

Reputation: 1582

Create a class for Person with first name and last name, then override ToString() method to output the string as you need.

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public override string ToString()
    {
        return FirstName + ", " + LastName;
    }
}

Now, you can create a list of people and bind to DataSource of comboBox.

var people = new List<Person>
{
    new Person() { FirstName = "John", LastName = "Doe" },
    new Person() { FirstName = "Nickel", LastName = "Back" }
};
comboBox1.DataSource = people;

Upvotes: 2

Related Questions