Reputation: 285
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.
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
Upvotes: 18
Views: 68358
Reputation: 1
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";
}
Upvotes: 0
Reputation: 1
// 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);
Upvotes: 0
Reputation: 1
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();
}
Upvotes: -1
Reputation: 1316
in C# 6 create readonly property in your Employee class
public string FullName=>$"{lastname} {firstname}";
then
cmbEmployees.DataSource = GetEmployees();
//something like below line which doesn't work
cmbEmployees.DisplayMember = "FullName";
cmbEmployees.ValueMember = "id";
Upvotes: 3
Reputation: 4358
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:
cmbEmployees.DisplayMember = "lastname";
Now, go to your form in a [Design] mode, right click on the ComboBox -> Properties.
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:
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 ;)
Upvotes: 65
Reputation: 6079
Your query should be like this in GetEmployees() function.
"SELECT id,(lastname + ' ' + first_name) AS NAME FROM TABLE"
cmbEmployees.DataSource = GetEmployees();
cmbEmployees.DisplayMember = "NAME";
cmbEmployees.ValueMember = "id";
Upvotes: 19
Reputation: 3404
Try one of those approaches:
Dictionary
with concatenated fields as value - https://stackoverflow.com/a/1006588/1816426Upvotes: 2
Reputation: 1536
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
.
Upvotes: 31