如何解决这个ComboBox项目设置问题?

时间:2010-10-26 19:12:46

标签: combobox c#-2.0

此代码未向组合框设置正确的值。这是因为,这些来自不同的背景。

此外,in my actual problem,在MyClass中既不重载== in也不重写Equals()也可以。

任何替代方式?

如何解决这个问题?

MyClass.cs

public class MyClass
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public MyClass(int id, string name)
        {
            ID = id;
            Name = name;
        }

        public static List<MyClass> Get()
        {
            return new List<MyClass>
            {
                new MyClass(2, "AAA"),
                new MyClass(4, "BBB"),
                new MyClass(5, "CCC"),
                new MyClass(7, "DDD"),
                new MyClass(10, "EEE")
            };
        }
    }

ComboBoxForm.cs

public partial class ComboBoxForm : Form
    {
        public ComboBoxForm()
        {
            InitializeComponent();
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            LoadDataToComboBoxFromDatabase();
        }

        private void LoadDataToComboBoxFromDatabase()
        {
            comboBox1.Items.Clear();

            List<MyClass> list = MyClass.Get();

            foreach(MyClass mc in list)
            {
                comboBox1.Items.Add(mc);
            }

            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "ID";

            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int ID = 5;//This is what I have.
            comboBox1.SelectedItem = .............how?
        }
    }

1 个答案:

答案 0 :(得分:1)

我相信您的Equals比较可能不正确。对于NHibernate,我使用以下Equals()方法:

public static class EntityUtil
{
    public static bool Equals(IEntity a, IEntity b)
    {
        // Transient entitys are never equal (unless they are ReferenceEquals)!

        return
            ReferenceEquals(a, b) || (
                !((object)a == null || (object)b == null) &&
                !(IsTransient(a) || IsTransient(b)) &&
                NHibernateProxyHelper.GetClassWithoutInitializingProxy(a) == NHibernateProxyHelper.GetClassWithoutInitializingProxy(b) &&
                a.Id.Equals(b.Id)
            );
    }

    public static bool IsTransient(IEntity entity)
    {
        return entity.Id == 0;
    }
}

我已经在一个单独的帮助器类中定义了这个方法,并在我的实体中使用它:

public class Entity : IEquatable<Entity>, IEquatable<IEntity>
{
    private int? _hashCode;

    public virtual int Id { get; protected set; }

    public override bool Equals(object obj)
    {
        return EntityUtil.Equals((IEntity)this, obj as IEntity);
    }

    public virtual bool Equals(Entity obj)
    {
        return EntityUtil.Equals((IEntity)this, (IEntity)obj);
    }

    public virtual bool Equals(IEntity obj)
    {
        return EntityUtil.Equals((IEntity)this, obj);
    }

    public static bool operator ==(Entity a, Entity b)
    {
        return EntityUtil.Equals((IEntity)a, (IEntity)b);
    }

    public static bool operator !=(Entity a, Entity b)
    {
        return !(a == b);
    }

    public override int GetHashCode()
    {
        // GetHashCode needs to return a stable value.

        if (!_hashCode.HasValue)
        {
            _hashCode =
                NHibernateProxyHelper.GetClassWithoutInitializingProxy(this).GetHashCode() ^
                ((IEntity)this).Id.GetHashCode();
        }

        return _hashCode.Value;
    }
}

这是与NHibernate相等的建议实现。也许这对你也有帮助。