Click here to Skip to main content
16,004,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello guys,
I was wondering if it was possible to show for example ID_REPORT and CLIENT_NAME on a combobox like this:

--------------------
1 - Client name v |
--------------------
Every report is associated with a client, and the best way to identify the reports are by using the client name. For many of the workers, they usually know the reports by client name, but they have a special code, and that code is really needed.

What I have tried:

I've tried to simply change the displaymember to use both columns like so:

cmbReportID.DisplayMember = "ID_REPORT" + "-" + "CLIENT_NAME"
Posted
Updated 12-May-17 5:40am
Comments
Richard Deeming 12-May-17 10:56am    
Probably simplest to change your query to return the combined value as a computed column, and use that as the display member.

There might be other options, but you haven't told us what UI framework you're using. (Windows Forms, WPF, Xamarin, ASP.NET WebForms, ASP.NET MVC, etc.)
Scribling Doodle 12-May-17 11:13am    
I'm using Winforms, but since I'm using * to select all the columns, how could I simply work out a joint between both columns? I also saw some c# things on this forum but all seem like Chinese to me tbh... Since I'm new to programming languages.
Richard Deeming 12-May-17 11:14am    
Don't use SELECT * FROM ...; just select the fields you actually want.
Scribling Doodle 12-May-17 11:16am    
For example "SELECT ID-REPORT + "var" + CLIENT_NAME FROM ..."?
Richard Deeming 12-May-17 11:18am    
SELECT 
    ID_REPORT, 
    CLIENT_NAME, 
    Convert(varchar(50), ID_REPORT) + ' - ' + CLIENT_NAME As DisplayName 
FROM 
    ...

1 solution

As discussed in the comments, the simplest solution is to modify the SQL query to add the combined value as a computed column:
SQL
SELECT 
    ID_REPORT, 
    CLIENT_NAME, 
    Convert(varchar(50), ID_REPORT) + ' - ' + CLIENT_NAME As DisplayName 
FROM 
    ...

You then set the display member to the name of the computed column:
VB.NET
cmbReportID.DisplayMember = "DisplayName"
 
Share this answer
 

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