tags:

views:

267

answers:

4

Hi,

I have this already populated ComboBox and all I want to do is to set it to a specific selectedItem knowing its value.

I'm trying this, but nothing happens:

comboPublisher.SelectedValue = livre.Editeur;

Considering the fact that I already implemented Equals(..) method in my class Editeur, this way:

  public  bool Equals(IEditeur editeur)
        {
            return (this.Nom == editeur.Nom);
        }

This is how I populate my ComboBox:

foreach (Business.IEditeur editeur in _livreManager.GetPublishers())
        {
            comboPublisher.Items.Add(editeur);
        }

Any idea ?

Thanks !

[EDIT]: This seems to work with :

comboPublisher.SelectedItem = livre.Editeur;

My Equals method is:

 public override bool Equals(object obj)
        {
            IEditeur editeur = new Editeur();

            if (!(obj is System.DBNull))
            {
                editeur = (IEditeur)obj;
                return (this.Nom == editeur.Nom);
            }

            return false;
        }
+1  A: 
In The Pink
+1  A: 

you've created a new implementation of Equals that hides the one in Object. Try declaring it with public override bool and see if that helps.

Dave
You're right I forgot override. It still doesn't solve the problem though :(
Amokrane
It works with SelectedItem instead of SelectedValue !
Amokrane
I always use SelectedItem... maybe that's why. :)
Dave
+2  A: 

Set the Text property.

AMissico
that only works if IsEditable is True, right?
Dave
I believe it works all the time, because you are setting programmatically. (I would have to verify.)
AMissico
A: 

In think that you have also to implement IEquatable in Editeur class, but passing an object as argument. Something like this. The rest of your code is fine.

public bool Equals(Editeur other)
{
    return (this.Nom == other.Nom);            
}

public override bool Equals(object obj)
{
    if (obj is Editeur)
    {
        return Equals(obj as Editeur);
    }
    return false;
}
Javier Morillo