gabsferreira
gabsferreira

Reputation: 3137

DisplayMember in Combobox not working properly

I have this method that adds Conta instances to a ComboBox called "comboContas":

public void AdicionaConta(Conta novaConta)
{
    comboContas.Items.Add(novaConta);
    comboContas.DisplayMember = "Titular";
}

Note that I've set the DisplayMember property to "Titular". Here is my Conta class:

public abstract class Conta
{
    public int Numero { get; set; }

    public double Saldo { get; set; }

    public Cliente Titular { get; set; }

    public override string ToString()
    {
        return "titular: " + this.Titular.Nome;
    }       
}

Now, "Titular" is of Cliente type. Here's Cliente class:

public class Cliente
{
    public string Nome { get; set; }
    public string Rg { get; set; }

    public Cliente(string nome)
    {
        this.Nome = nome;
    }

    public override string ToString()
    {
        return "ToString do Cliente: " + this.Nome;
    }
}

What I'd like to show in the "comboContas" ComboBox is something like "ToString do Cliente: Gabriel". However, the ToString method of the Cliente class is not being called. Instead, the one being called is from the Conta class.

enter image description here

This is pretty simple stuff and I really don't know what's happening. If I change DisplayMember to any other property, it works. If I change the type of the "Titular" property to any other type, the ToString() of this other type is called. It just doesn't work with Cliente.

Upvotes: 1

Views: 1317

Answers (2)

Ivan Stoev
Ivan Stoev

Reputation: 205579

Something is wrong with your code (setting it after every add instead of in advance?) because it indeed works as expected. Check it out:

using System;
using System.Windows.Forms;

namespace Tests
{
    public class Conta
    {
        public int Numero { get; set; }
        public double Saldo { get; set; }
        public Cliente Titular { get; set; }
        public override string ToString()
        {
            return "titular: " + this.Titular.Nome;
        }
    }
    public class Cliente
    {
        public string Nome { get; set; }
        public string Rg { get; set; }
        public Cliente(string nome)
        {
            this.Nome = nome;
        }
        public override string ToString()
        {
            return "ToString do Cliente: " + this.Nome;
        }
    }
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form { Padding = new Padding(16) };
            var comboBox = new ComboBox { Dock = DockStyle.Top, Parent = form };
            comboBox.DisplayMember = "Titular";
            comboBox.Items.AddRange( new []
            {
                new Conta { Titular = new Cliente("Victor") },
                new Conta { Titular = new Cliente("Mauricio") },
                new Conta { Titular = new Cliente("csni") },
            });
            Application.Run(form);
        }
    }
}

Result:

enter image description here

Upvotes: 4

Max Ouellet
Max Ouellet

Reputation: 306

I'm not 100% sure why what you are doing isn't working, but one way that would likely work is to do something like this:

public class Client
{
    public string DisplayName { get { return "ToString do Client: " + this.Nome; } }
}

Then, just change your combo box to bind the DisplayMember to DisplayName:

public void AdicionaConta(Conta novaConta)
{
    comboContas.Items.Add(novaConta);
    comboContas.DisplayMember = "Titular.DisplayName";
}

I hope this helps!

Upvotes: 0

Related Questions