views:

224

answers:

2

In the app I'm working on, I need to maintain a list of Projects which are currently loaded, and display the names of each one in a ListBox (okay, multiple ListBoxes, but that's neither here nor there).

class Project
{
    public String Name;

    // Etc. etc...
}

I've got a BindingList object which contains all of the loaded Projects, and am binding it to my ListBox(es).

private BindingList<Project> project_list = new BindingList<Project>();
private ListBox project_listbox;

private void setList()
{
    project_listbox.DisplayMember = "Name";
    project_listbox.ValueMember = "Name";
    project_listbox.DataSource = project_list;
}

However when I do this, all that gets displayed in project_listbox is a set of the class names for the Project. Am I missing something here? Every reference that I could find regarding binding Lists to a ListBox uses a very similar setup.

+1  A: 
CasperT
+1 for the property, but forget the readonly field. DisplayMember has to be the name of a property.
Henk Holterman
Thanks. You are right. I went and tested it.
CasperT
Thanks. This works, though I'm unclear as to why this is functionally different than what I had previously. Does the ValueMember property only work when the `set` is private?
themarshal
No no, just a habit :) You normally wouldn't want other classes to simply to set your properties to whatever the feel like. It is practically "readonly" I have applied to the property.
CasperT
A: 

As an aside, note that if you want to search on or sort the BindingList, you will need to implement it and add this functionality

http://msdn.microsoft.com/en-us/library/ms993236.aspx

splatto