Beginner Data binding question

D

David H

I'm getting stumped on what should be a simple problem.

I have data class (ActionList) that inherits from CollectionBase and
implements the abstract members. I want to be able to bind an instance
of this class to a ListBox control so that the updates to the
underlying ActionList will be reflected in the ListBox.

So far, the closest I can get is something along these lines:

ActionList list=new ActionList();
ListBox1.DataSource=list;
list.Add(new (ActionItem("Action!"));
ListBox1.DisplayMember="ActionName";

This (sorta) works. In order to get the ListBox to update, I must do
this:
LisBox1.DataSource=null;
ListBox1.DataSource=list;

The other problem is that despite the fact that I'm setting the
DisplayMemeber, all I get is the default string representation of the
item :(.

Can someone tell me what I'm missing here?

Thanks in advance,

David
 
S

sloan

If you're working in winforms (not asp.net) (that I am assuming)

#1 Rule

You are putting objects in the listbox (or combobox or whatever)
Again, you're putting objects, and not named value pairs like in the web.

public class Employee


public class EmployeeCollection : List<Employee> (or collection base in 1.1)


if you find your listbox to an instance of EmployeeCollection, what is in
listbox are instances of the Employee object.

public class Employee
public override ToString()
return "See what I mean?";


that is pseudo code, you'll have to work it out.

One way to get what you want is to override the ToString() method.
 
K

Ken Foskey

This (sorta) works. In order to get the ListBox to update, I must do
this:
LisBox1.DataSource=null;
ListBox1.DataSource=list;

ListBox1.Invalidate(); // or refresh google group for difference
The other problem is that despite the fact that I'm setting the
DisplayMemeber, all I get is the default string representation of the
item :(.

I am assuming a typo above, you should check that :)

Is DisplayMember a get set value or a simple value in your class? Is it
available to the listbox1 instance?

Ta
Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top