如何在 C# 中的组合框显示成员中附加两个字段值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14679253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to append two field values in combobox display member in C#
提问by MekeniKine
In my table, I have a field of firstname
and lastname
, now what I want is to set firstname
and lastname
as displaymember
in a combobox, but I don't know how to do it.
在我的表中,我有一个firstname
and字段lastname
,现在我想要设置firstname
和lastname
作为displaymember
组合框,但我不知道该怎么做。
Something like this
像这样的东西
cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "lastname, first_name";
cmbEmployees.ValueMember = "id";
How can I achieve this? So that both lastname
and firstname
will be displayed in the combobox
我怎样才能做到这一点?这样lastname
和firstname
都将显示在combobox
采纳答案by Mash
Let's say you had a class like this:
假设你有一个这样的类:
class Person
{
public string FirstName
{
get;
set;
}
public string LastName
{
get;
set;
}
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
public Person(string firstname, string lastname)
{
FirstName = firstname;
LastName = lastname;
}
}
If you don't have a FullName
property, just create one in the format you wish to display the name. Then set the DisplayMember
equal to FullName
.
如果您没有FullName
属性,只需以您希望显示名称的格式创建一个。然后设置DisplayMember
等于FullName
。
回答by Konrad Gadzina
Try one of those approaches:
尝试其中一种方法:
- new
Dictionary
with concatenated fields as value - https://stackoverflow.com/a/1006588/1816426 - calculated column - https://stackoverflow.com/a/1006546/1816426
- 新
Dictionary
的连接字段作为值 - https://stackoverflow.com/a/1006588/1816426 - 计算列 - https://stackoverflow.com/a/1006546/1816426
回答by andy
Your query should be like this in GetEmployees()function.
您的查询在GetEmployees()函数中应该是这样的。
"SELECT id,(lastname + ' ' + first_name) AS NAME FROM TABLE"
cmbEmployees.DataSource = GetEmployees();
cmbEmployees.DisplayMember = "NAME";
cmbEmployees.ValueMember = "id";
回答by Eliran Kuta
This example will guide you how to do that without modifying your base class.
此示例将指导您如何在不修改基类的情况下执行此操作。
First, you can leave your DisplayMember with one property, let's say:
首先,您可以为 DisplayMember 保留一个属性,例如:
cmbEmployees.DisplayMember = "lastname";
Now, go to your form in a [Design] mode, right click on the ComboBox -> Properties.
现在,在 [设计] 模式下转到您的表单,右键单击 ComboBox -> 属性。
In the top of the Properties window, click on Events (lightning icon),
在“属性”窗口的顶部,单击“事件”(闪电图标),
look for Format in the events list below (under Property Changed) and type there some event name, let's say: ComboBoxFormat , and press Enter. You will see this:
在下面的事件列表中查找 Format(在 Property Changed 下)并在那里输入一些事件名称,例如: ComboBoxFormat ,然后按 Enter。你会看到这个:
private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
{
}
And now write these following lines inside:
现在在里面写下以下几行:
private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
{
// Assuming your class called Employee , and Firstname & Lastname are the fields
string lastname = ((Employee)e.ListItem).Firstname;
string firstname = ((Employee)e.ListItem).Lastname;
e.Value = lastname + " " + firstname;
}
That's it ;)
就是这样 ;)
回答by Hisham
in C# 6 create readonly property in your Employee class
在 C# 6 中,在您的 Employee 类中创建只读属性
public string FullName=>$"{lastname} {firstname}";
then
然后
cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "FullName";
cmbEmployees.ValueMember = "id";
回答by Sufiyan Ghazi
CREATE VIEW [dbo].[get_view] AS SELECT CONCAT(sell_tb.Name,extra_tb.Name,purchase_tb.Name) AS Name FROM sell_tb FULL JOIN extra_tb ON extra_tb.E_ID = sell_tb.E_ID FULL JOIN purchase_tb ON purchase_tb.S_ID = sell_tb.S_ID;
CREATE VIEW [dbo].[get_view] AS SELECT CONCAT(sell_tb.Name,extra_tb.Name,purchase_tb.Name) AS Name FROM sell_tb FULL JOIN extra_tb ON extra_tb.E_ID = sell_tb.E_ID FULL JOIN purchase_tb ON purchase_tb = sell_tb S_ID;
private void alldata1()
{
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from [get_view]";
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "Name";
conn.Close();
}