ComboBox Display Member on multiple fields

multi column combobox windows forms
c# combobox displaymember concatenate
multi column combo box
listbox displaymember multiple fields
c# listbox displaymember concatenate
multicolumncombobox

I'm trying to set the.DisplayMember property of a ComboBox in C#, but I want to bind it to multiple columns in the .DataSouce.

My SQL looks like this:

SELECT PersNbr, PersFirstName, PersMiddleName, PersLastName
FROM Pers WHERE PersNbr = :persNbr;

I'm saving this query in a DataTable so each column selected has it's own column in the Datatable.

I want to make the .DisplayMember a combination of PersFirstName + PersMiddleName + PersLastName so their full name appears like this:

comboBox.DisplayMemeber = "PersFirstName" + "PersMiddleName" + "PersLastName"

I know I can just to this on the query:

SELECT PersNbr, (PersFirstName || PersMiddleName || PersLastName) PersName

and then just do this:

comboBox.DisplayMember = "PersName";

but I don't want to do the formatting of data in the database layer since it's not supposed to be there.

How else can I achieve this in Winforms?

You can create an expression column and then use it as a DisplayMember:

dataTable.Columns.Add(
    "FullName", 
    typeof(string), 
    "PersFirstName + ' ' + PersMiddleName + ' ' PersLastName");

comboBox.DisplayMember = "FullName";

How to append two field values in combobox display member in C# , Let's say you had a class like this: class Person { public string FirstName { get; set​; } public string LastName { get; set; } public string FullName  It use to work (My old Combobox still display the 2 fields) and even when i Copy-Paste them. But when i create one from scratch, it won't display multiple fields no matter what i do.

Don't use heavy DataTable only for transporting data from database to the code. Create a class, load data in it then you will be able format data with full support of programming language you are using (functions and etc.)

public class Person
{
    public int Number { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }

    public string Name => $"{FirstName} {MiddleName} {LastName}"; 
}

// Load data
var persons = new List<Person>();

using (var connection = new SqlConnection(connectionsString))
using (var command = connection.CreateCommand())
{
    command.CommandText = "SELECT PersNbr, PersFirstName, PersMiddleName, PersLastName FROM Pers";
    connection.Open();

    using (var reader = command.ExecuteReader())
    {
        while (reader.Read())
        {
            var person = new Person
            {
                Number = reader.GetInt32(0),
                FirstName = reader.GetString(1),
                MiddleName = reader.GetString(2),
                LastName = reader.GetString(3),
            };
            persons.Add(person);
        }
    }
}

combobox.DisplayMember = "Name";
combobox.DataSource = persons;

Multiple table fields as "DisplayMember" in a combobox?, I want the dropdown to display (DisplayMember) firstname and lastname. Is that possible? I can't choose more than one table field :( Using only  In this article I explain how to bind multiple column values into a ComboBox. It is very simple but I have often bound a single column in a ComboBox so it was a bit of a curiosity for me. Use the following procedure to bind to a combo box. Step 1. Open a Windows Forms application and insert a ComboBox and a button control on the form from the

If you are using a datatable you should do this process on database layer. I've recommend you to do this by using POCO classes. But if you really want to do this on application layer and by using datatable here is the following code:

dataTable.Columns.Add("PersName");
foreach(DataRow item in dataTable.Rows)
{
    item["PersName"] =  item["PersFirstName"] + item["PersMiddleName"] + ["PersLastName"]
}

Then you can set the DisplayMember propert to "PersName".

Thread: how to display two column value in combobox DisplayMember, hi i'm having hard time on how to display two column values in combox display member..like custID and custNameany sample code plz help. I'm trying to set the.DisplayMember property of a ComboBox in C#, but I want to bind it to multiple columns in the .DataSouce. My SQL looks like this: SELECT PersNbr, PersFirstName, PersMiddleName, PersLastName FROM Pers WHERE PersNbr = :persNbr; I'm saving this query in a DataTable so each column selected has it's own column in the Datatable.

Display-multiple-members | WindowsForms, How to Display Multiple Members in a MultiColumnComboBox In order to display the contents of the entire row in the MultiColumnComboBox instead of the using DisplayMember property, can achieve by handling Display-multiple-​fields. display multiple values in combo box access I am trying to show multiple fields in my combo box on my form. When I click the drop down arrow is shows both columns, however after selection only the information from the first column is displayed.

Multiple field DisplayMember in UI for WinForms DropDownList , Join a community of over 2.6m developers to have your questions answered on Multiple field DisplayMember of UI for WinForms DropDownList  Being able to see one field on its own in a drop down is useless. I need to filter results from the equipment table based on a site ID (integer) then display the multiple fields per row. In access this was easy but powerapps seems to only let you use 1 value.

COMBOBOX more than one field for the display member ?, Then use that field as the display member for the combo box. and want to display the values of two columns in the cbo (as displaymember). 1. The SharePoint "Rate" list field is set to accept multiple values. 2. The ComboBox card from the Powerapps form is of type " Edit MultiSelect" (see the picture below) That should solve the first issue. For the second issue, make sure to use the "SelectedItems" property instead of "Selected.

Comments
  • Possible duplicate of DisplayMemberPath concatenation
  • @zambonee that is XAML. He asks for a windows forms. As much as i know you can't set a combobox item template in windows forms. Maybe there might be an event fired on DataPopulated or something like that. But that should't be the way we follow
  • Wow, i didn't know there is something such as Expression Column. That's interesting
  • Is it possible to use a function inside the expression? If PersMiddleName is null in my DB, an extra space is added. Is there a quick way to solve this, or would I need to evaluate the individual row values.
  • @Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…