在我的表格中,我有一个firstname
和lastname
字段,现在我想要的是将firstname
和lastname
设置为displaymember
组合框,但我不知道怎么做。
像这样的东西
cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "lastname, first_name";
cmbEmployees.ValueMember = "id";
我怎样才能做到这一点?因此lastname
和firstname
都会显示在combobox
答案 0 :(得分:46)
此示例将指导您如何在不修改基类的情况下执行此操作。
首先,你可以给你的DisplayMember留下一个属性,比如说:
cmbEmployees.DisplayMember = "lastname";
现在,转到[设计]模式中的表单, 右键单击ComboBox - >属性。
在“属性”窗口的顶部,单击“事件(闪电图标)”,
在下面的事件列表中查找格式(在Property Changed下)并输入一些事件名称,例如: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;
}
就是这样;)
答案 1 :(得分:24)
假设您有一个这样的课程:
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
。
答案 2 :(得分:18)
您的查询在 GetEmployees()函数中应该是这样的。
"SELECT id,(lastname + ' ' + first_name) AS NAME FROM TABLE"
cmbEmployees.DataSource = GetEmployees();
cmbEmployees.DisplayMember = "NAME";
cmbEmployees.ValueMember = "id";
答案 3 :(得分:3)
在C#6中,在Employee类中创建readonly属性
public string FullName=>$"{lastname} {firstname}";
然后
cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "FullName";
cmbEmployees.ValueMember = "id";
答案 4 :(得分:2)
尝试以下方法之一:
Dictionary
,其中包含连接字段值 - https://stackoverflow.com/a/1006588/1816426 答案 5 :(得分:0)
创建视图[dbo]。[get_view] 如 SELECT CONCAT(sell_tb.Name,extra_tb.Name,purchase_tb.Name)AS名称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();
}
答案 6 :(得分: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);