Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ComboBox Display Member on multiple fields

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?

like image 658
Jimenemex Avatar asked Dec 14 '22 15:12

Jimenemex


1 Answers

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";
like image 163
hardkoded Avatar answered Dec 27 '22 11:12

hardkoded