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";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With