views:

277

answers:

2

Well, my question is a bit silly, but I've tried lots of different thing without result.

I have a ComboBox in my main form and I want to point its data source to the public readonly List PriceChanges list declared in the Filters class. No problem with that but I want to list the Description field.

I tried to assign the "Description" string to the DisplayMember attribute without success. My ComboBox only lists: "BusinessLogic.PriceChange" for each entry, where BusinessLogic is the name of my Namespace and PriceChange the class.

I appreciate any help.

Regards

That's part of the code of my main form

    public mainFrm()
    {
        InitializeComponent();

        prodFilter = new Filters();
        cbPriceChanges.DataSource = prodFilter.PriceChanges;
        cbPriceChanges.DisplayMember = "Description"
    }

That's is part of the code that declares the List object

public enum PriceChangeTypes
{
    No_Change,
    Increased,
    Decreased,
    All
}

public class PriceChange
{
    public String Description;
    public readonly PriceChangeTypes Type;

    public delegate bool ComparisonFuntionDelegate(Decimal a);
    public readonly ComparisonFuntionDelegate ComparisonFunction;

    public PriceChange(String Description, PriceChangeTypes type , ComparisonFuntionDelegate CompFunc)
    {
        this.Description = Description;
        Type = type;
        ComparisonFunction = CompFunc;
    }
}

public class Filters
{

    public readonly List<PriceChange> PriceChanges = null;

    public Filters()
    {
        PriceChanges = new List<PriceChange>();

        PriceChanges.Add(new PriceChange("No Change", PriceChangeTypes.No_Change, PriceChange => PriceChange == 0));
        PriceChanges.Add(new PriceChange("Increased", PriceChangeTypes.Increased, PriceChange => PriceChange > 0));
        PriceChanges.Add(new PriceChange("Decreased", PriceChangeTypes.Decreased, PriceChange => PriceChange < 0));
        PriceChanges.Add(new PriceChange("All", PriceChangeTypes.All, a => true));
    }
}
+1  A: 

Have you tried making "Description" a property? It will change a lot in case the list tries to get the field through reflection (as it most likely does).

public class PriceChange {
    public string Description{
        get;
        set;
    }
    // ...
}
Paolo Tedesco
Perfect, that's it, I think I need to deep my skills on how this thing behave internally. Cheers mate
Andres
Glad it helped :)
Paolo Tedesco
A: 

Try adding this to your class :

public override string ToString()
        {
            return Description;
        }

Currently you're just getting the default value of ToString, which is the object namespace and class

CodeByMoonlight