Domanda

I have the following code

clbCodes.DisplayMember = "Name";
clbCodes.ValueMember = "Id";

checkboxItemList = new List<CheckBoxItem>();
foreach (var uagCode in codes)
{
    var checkboxItem = new CheckBoxItem
        {
            Id = uagCode.Code,
            Name = uagCode.UAGLabel
        };
    checkboxItemList.Add(checkboxItem);
}

clbCodes.DataSource = checkboxItemList;

public class CheckBoxItem
{
    public string Name { get; set; }
    public string Id { get; set; }
}

However, when I run this, instead of seeing the "Name" of my item, e.g.,

"Card"
"Toy"
"Table"

I see

WindowsApplication1.CheckBoxItem 
WindowsApplication1.CheckBoxItem 
WindowsApplication1.CheckBoxItem 

populated in my listbox

What did I do wrong?

È stato utile?

Soluzione

Since it appears that you are adding to the CheckListBox a collection of custom objects, you should have as part of that class an override of ToString() that can return the Name that you are wanting to display.

public override String ToString()
{
    return this.Name;
}

Altri suggerimenti

You need to override ToString:

public class CheckBoxItem
{
    public string Name { get; set; }
    public string Id { get; set; }

    public override String ToString()
    {
       return Name;
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top