tags:

views:

4114

answers:

8

I have a generic list, i.e. List<myclass>. Here myclass contains two string properties.

How can I assign a datasource to the list collection?

A: 

You got it the other way around. Databound objects like grids and the like could set generic lists as their data source.

You have to either manually populate your list or use a technology that populates it for you (e.g., LINQ to SQL, NHibernate)

Jon Limjap
+1  A: 

You can't. That's because a List is no IBindableComponent. A Windows Forms is: See MSDN Control Class.

Mudu
+6  A: 

Mirmal, I guess English is not your first language, this question is not very clear. I think that what you are asking is given a list of your class how do you then bind that list to something (a listbox or combobox etc)

Here is a simple code snippet of how to do this...

private void button2_Click(object sender, EventArgs e)
{
  List<MyClass> list = new List<MyClass>();
  list.Add(new MyClass() { FirstName = "Tim", Lastname = "Jarvis"});
  list.Add(new MyClass() { FirstName = "John", Lastname = "Doe" });

  listBox1.DataSource = list;
  listBox1.DisplayMember = "FirstName"; // or override MyClass's ToString() method.
}

I hope this has answered your question.

Tim Jarvis
A: 

i need the reverse process of this from listbox to list collection

ok, I have a question then, what is the datasource for your listbox?
Tim Jarvis
A: 

You do not assign a datasource to a List<> object. You can use a List<> as a datasource for a user interface control though.

If you want to make you could derive from List<> and implement IBindableComponent which would allow you to provide mechanisms for databinding to a list. This is almost certaintly not the best way to go about achieving what you want to do though.

Edit: If you have a control and want to retrieve the datasource and you know it's a List<> object you can just do:

List<MyClass> lst = listBox1.DataSource as List<MyClass>;
spoon16
A: 

Assuming you have set the Datasource of the listbox to a list:

List<MyClass> list = listBox1.DataSource;

Otherwise, iterate over the items in listBox1.DataSource, converting each one and add to your List.

Mitch Wheat
+3  A: 

Start with a simple class:

    // create a dummy class
    public class MyClass
    {
        private string name;
        public MyClass(string name)
        {
            ItemName = name;
        }
        public string ItemName
        {
            get { return name; }
            set { name = value; }
        }
    }

Create a binding list and add some classes to the list:

    // create a binding list
    BindingList<MyClass> my_list = new BindingList<MyClass>();

    // add some clssses to the list
    my_list.Add(new MyClass("Item #1"));
    my_list.Add(new MyClass("Item #2"));

Bind the list to the listbox datasource indicating which class property is to be used in the listbox display:

    // make the list the datasource for a listbox
    listBox1.DataSource = my_list;

    // this is the property of the class displayed in the listbox
    listBox1.DisplayMember = "ItemName";
jussij
+2  A: 

Hi!

You can wrap your list into a binding list:

System.ComponentModel.BindingList<myClass> bindingList = new System.ComponentModel.BindingList<myClass>(originalList);

Goran