如何在C#中的组合框显示成员中附加两个字段值

本文关键字:字段 两个 成员 组合 显示 | 更新日期: 2023-09-27 17:57:30

在我的表中,我有一个字段firstnamelastname,现在我想在一个组合框中将firstnamelastname设置为displaymember,但我不知道如何做到。

像这样的

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

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

如何在C#中的组合框显示成员中附加两个字段值

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

首先,您可以将DisplayMember留给一个属性,比方说:

cmbEmployees.DisplayMember = "lastname";

现在,以[Design]模式转到您的表单,右键单击组合框->属性。

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

在下面的事件列表(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;
}

就是这样;)

假设您有一个这样的类:

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

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

"SELECT id,(lastname + ' ' + first_name) AS NAME FROM TABLE"
cmbEmployees.DataSource = GetEmployees();
cmbEmployees.DisplayMember = "NAME"; 
cmbEmployees.ValueMember = "id";
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";

尝试其中一种方法:

  • 具有串联字段作为值的新Dictionaryhttps://stackoverflow.com/a/1006588/1816426
  • 计算列-https://stackoverflow.com/a/1006546/1816426
// 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);
    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";
    }

CREATE VIEW[dbo]。[get_view]作为选择CONCAT(sell_tb.Name,extra_tb.Name,purchase_tb.Name)作为名称FROM sell_tb FULL JOIN extra_tb ON extra_tb.E_ID=sell_tb.E_ID在purchase_tb.S_ID=sell_tb_S_ID上完全加入purchase_db;

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