如何在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:39:34  来源:igfitidea点击:

How to append two field values in combobox display member in C#

c#winformscombobox

提问by MekeniKine

In my table, I have a field of firstnameand lastname, now what I want is to set firstnameand lastnameas displaymemberin a combobox, but I don't know how to do it.

在我的表中,我有一个firstnameand字段lastname,现在我想要设置firstnamelastname作为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 lastnameand firstnamewill be displayed in the combobox

我怎样才能做到这一点?这样lastnamefirstname都将显示在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 FullNameproperty, just create one in the format you wish to display the name. Then set the DisplayMemberequal to FullName.

如果您没有FullName属性,只需以您希望显示名称的格式创建一个。然后设置DisplayMember等于FullName

回答by Konrad Gadzina

Try one of those approaches:

尝试其中一种方法:

回答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();
    }