如何将数据源分配给通用列表集合?

时间:2021-11-10 13:11:41

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

我有一个通用列表,即List 。这里myclass包含两个字符串属性。

How can I assign a datasource to the list collection?

如何将数据源分配给列表集合?

6 个解决方案

#1


11  

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)

Mirmal,我猜英语不是你的第一语言,这个问题不是很清楚。我认为你要问的是你的班级列表然后如何将该列表绑定到某个东西(列表框或组合框等)

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.

我希望这已经回答了你的问题。

#2


4  

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:

将列表绑定到listbox数据源,指示在列表框显示中使用哪个类属性:

    // 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";

#3


2  

You can wrap your list into a binding list:

您可以将列表包装到绑定列表中:

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

Goran

#4


1  

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

你不能。那是因为List不是IBindableComponent。 Windows窗体是:请参阅MSDN控件类。

#5


1  

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

您没有将数据源分配给List <>对象。您可以使用List <>作为用户界面控件的数据源。

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.

如果你想让你可以从List <>派生并实现IBindableComponent,它允许你提供数据绑定到列表的机制。这几乎肯定不是实现你想做的最好的方法。

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 <>对象,你可以这样做:

List<MyClass> lst = listBox1.DataSource as List<MyClass>;

#6


0  

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)

您必须手动填充列表或使用为您填充它的技术(例如,LINQ to SQL,NHibernate)

#1


11  

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)

Mirmal,我猜英语不是你的第一语言,这个问题不是很清楚。我认为你要问的是你的班级列表然后如何将该列表绑定到某个东西(列表框或组合框等)

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.

我希望这已经回答了你的问题。

#2


4  

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:

将列表绑定到listbox数据源,指示在列表框显示中使用哪个类属性:

    // 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";

#3


2  

You can wrap your list into a binding list:

您可以将列表包装到绑定列表中:

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

Goran

#4


1  

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

你不能。那是因为List不是IBindableComponent。 Windows窗体是:请参阅MSDN控件类。

#5


1  

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

您没有将数据源分配给List <>对象。您可以使用List <>作为用户界面控件的数据源。

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.

如果你想让你可以从List <>派生并实现IBindableComponent,它允许你提供数据绑定到列表的机制。这几乎肯定不是实现你想做的最好的方法。

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 <>对象,你可以这样做:

List<MyClass> lst = listBox1.DataSource as List<MyClass>;

#6


0  

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)

您必须手动填充列表或使用为您填充它的技术(例如,LINQ to SQL,NHibernate)