17

在我的表中,我有一个 and 字段firstnamelastname现在我想要在组合框中设置firstnamelastnamedisplaymember但我不知道该怎么做。

像这样的东西

cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "lastname, first_name";     
cmbEmployees.ValueMember = "id";

我怎样才能做到这一点?这样lastnamefirstname都将显示在combobox

4

8 回答 8

60

此示例将指导您如何在不修改基类的情况下执行此操作。

首先,您可以为 DisplayMember 保留一个属性,例如:

cmbEmployees.DisplayMember = "lastname";

现在,在 [Design] 模式下转到您的表单,右键单击 ComboBox -> Properties。

在属性窗口的顶部,单击事件(闪电图标),

在下面的事件列表中(在 Property Changed 下)查找 Format 并在那里输入一些事件名称,比如说: ComboBoxFormat ,然后按 Enter。你会看到这个:

private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
{

}

现在在里面写下以下几行:

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;
}

而已 ;)

于 2013-09-15T20:49:16.840 回答
28

假设您有这样的课程:

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;
    }
}

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

于 2013-02-04T01:36:12.703 回答
19

您的查询在GetEmployees()函数中应该是这样的。

"SELECT id,(lastname + ' ' + first_name) AS NAME FROM TABLE"

cmbEmployees.DataSource = GetEmployees();
cmbEmployees.DisplayMember = "NAME"; 
cmbEmployees.ValueMember = "id";
于 2013-02-04T06:51:22.613 回答
3

在 C# 6 中,在您的 Employee 类中创建只读属性

public string FullName=>$"{lastname} {firstname}";

然后

cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "FullName";     
cmbEmployees.ValueMember = "id";
于 2016-10-24T22:29:10.067 回答
2

尝试其中一种方法:

于 2013-02-04T01:36:24.153 回答
0
    public void alldata1()
    {
        var Person= context.Person.Select(s => new { 
            display = s.surName+" "+s.name,
            value = s.studentID
        });
        comboBoxStudentNom.DataSource = Person.ToList();
        comboBoxStudentNom.ValueMember = "value";
        comboBoxStudentNom.DisplayMember = "display";
    }
于 2021-10-14T09:41:39.360 回答
0

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;

      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();
    }
于 2019-02-28T20:45:53.197 回答
0
// Declare a class
private class ComboRec
   {
     public string ID { get; set; }
     public string FirstName { get; set; }
     public string LastName { get; set; }
     public string FullName { get; set; }
     public string Department { get; set; }
   }

   

    // Fill the Combo Items
    private void FillMyComboList()
            {
                EmployeesCombo.Items.Clear();
                EmployeesCombo.ValueMember = "ID";
                EmployeesCombo.DisplayMember = "FullName";
    
                MySqlCommand cmd = new MySqlCommand("select id, firstname, lastname, department from employees order by lastname, firstname", MyConnection);
                cmd.Transaction = myTransaction;
                MySqlDataReader rdr = cmd.ExecuteReader();
    
                ComboRec comborec;
    
                while (rdr.Read())
                {
                    comborec = new ComboRec();
                    comborec.ID = rdr["id"].ToString();
                    comborec.FirstName = rdr["firstname"].ToString();
                    comborec.LastName = rdr["lastname"].ToString();
                    comborec.FullName = rdr["lastname"].ToString() + ", " + rdr["firstname"].ToString();
                    comborec.Department = rdr["department"].ToString();
    
                    EmployeesCombo.Items.Add(comborec);
                }
    
                rdr.Close();
            }   
       
       
    // Get the values from combo
    string id = ((ComboRec)EmployeesCombo.SelectedItem).ID);
    string firstname = ((ComboRec)EmployeesCombo.SelectedItem).FirstName);
    string lastname = ((ComboRec)EmployeesCombo.SelectedItem).LastName);
    string fullname = ((ComboRec)EmployeesCombo.SelectedItem).FullName);
    string department = ((ComboRec)EmployeesCombo.SelectedItem).Department);
于 2020-12-17T14:00:01.067 回答