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?
c# winforms data-binding combobox formatting
add a comment |
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?
c# winforms data-binding combobox formatting
Possible duplicate of DisplayMemberPath concatenation
– zambonee
Aug 26 '18 at 23:21
1
@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
– ArgeKumandan
Aug 26 '18 at 23:25
add a comment |
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?
c# winforms data-binding combobox formatting
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?
c# winforms data-binding combobox formatting
c# winforms data-binding combobox formatting
edited Aug 27 '18 at 13:48
Jimenemex
asked Aug 26 '18 at 23:13
JimenemexJimenemex
2,1071525
2,1071525
Possible duplicate of DisplayMemberPath concatenation
– zambonee
Aug 26 '18 at 23:21
1
@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
– ArgeKumandan
Aug 26 '18 at 23:25
add a comment |
Possible duplicate of DisplayMemberPath concatenation
– zambonee
Aug 26 '18 at 23:21
1
@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
– ArgeKumandan
Aug 26 '18 at 23:25
Possible duplicate of DisplayMemberPath concatenation
– zambonee
Aug 26 '18 at 23:21
Possible duplicate of DisplayMemberPath concatenation
– zambonee
Aug 26 '18 at 23:21
1
1
@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
– ArgeKumandan
Aug 26 '18 at 23:25
@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
– ArgeKumandan
Aug 26 '18 at 23:25
add a comment |
3 Answers
3
active
oldest
votes
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";
1
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
Is it possible to use a function inside the expression? IfPersMiddleName
isnull
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
Aug 27 '18 at 14:10
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
add a comment |
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".
add a comment |
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;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52030908%2fcombobox-display-member-on-multiple-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
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";
1
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
Is it possible to use a function inside the expression? IfPersMiddleName
isnull
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
Aug 27 '18 at 14:10
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
add a comment |
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";
1
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
Is it possible to use a function inside the expression? IfPersMiddleName
isnull
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
Aug 27 '18 at 14:10
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
add a comment |
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";
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";
answered Aug 26 '18 at 23:26
hardkodedhardkoded
6,10522029
6,10522029
1
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
Is it possible to use a function inside the expression? IfPersMiddleName
isnull
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
Aug 27 '18 at 14:10
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
add a comment |
1
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
Is it possible to use a function inside the expression? IfPersMiddleName
isnull
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
Aug 27 '18 at 14:10
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
1
1
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
Wow, i didn't know there is something such as Expression Column. That's interesting
– ArgeKumandan
Aug 26 '18 at 23:28
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
Aug 27 '18 at 14:10
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
Aug 27 '18 at 14:10
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
@Jimenemex you have the list of available options in the doc docs.microsoft.com/en-us/dotnet/api/…
– hardkoded
Aug 27 '18 at 14:17
add a comment |
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".
add a comment |
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".
add a comment |
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".
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".
answered Aug 26 '18 at 23:22
ArgeKumandanArgeKumandan
576617
576617
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 22 '18 at 4:59
FabioFabio
20.2k22048
20.2k22048
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52030908%2fcombobox-display-member-on-multiple-fields%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of DisplayMemberPath concatenation
– zambonee
Aug 26 '18 at 23:21
1
@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
– ArgeKumandan
Aug 26 '18 at 23:25