In my table, I have a field of firstname
and lastname
, now what I want is to set firstname
and lastname
as displaym
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 ;)
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
.
// 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);
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";
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";
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();
}