How to have more than one column shown in a Combobox?

Asked

Viewed 328 times

0

The question is the same as the title, I have a ComboBox called txtProfissional and set up the same way:

txtProfissional.DataSource = modelOff.profissionals.Where(p => p.idUnidade == oUsuario.unidade);
txtProfissional.DisplayMember = "nome";
txtProfissional.ValueMember = "id";

My intention is that on the property DisplayMember the columns are listed nome and funcao.

Any hint?

1 answer

3


Create a property that joins name and function.

Supposing Profissional be the class name.

public class Profissional
{
    // outras propriedades
    public string NomeFuncao => $"{nome} - {funcao}";
}
txtProfissional.DisplayMember = "NomeFuncao";
txtProfissional.ValueMember = "id";

If you don’t want to create the property, for whatever reason, you can create an event Format for control

private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
{        
    var item = (Profissional)e.ListItem;
    e.Value = "{item.Nome} - {item.Funcao}";
}
  • I access data from a database, and there is no such column.

  • 1

    That’s why I told you to create the property, @Italorodrigo

Browser other questions tagged

You are not signed in. Login or sign up in order to post.