How to have more than one column being displayed in a ComboBox?

0

The question is the same as the title, I have a ComboBox called txtProfissional and I set the same thing like this:

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

My intention is that the DisplayMember property will list the nome and funcao columns.

Any tips?

    
asked by anonymous 12.06.2017 / 20:27

1 answer

3

Create a property that merge name and function.

Assuming Profissional is the class name.

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

If you do not want to create the property, for whatever reason, you can create a Format event for the control

private void ComboBoxFormat(object sender, ListControlConvertEventArgs e)
{        
    var item = (Profissional)e.ListItem;
    e.Value = "{item.Nome} - {item.Funcao}";
}
    
12.06.2017 / 20:30