DataSource的数据源

使用DataSource属性指定要绑定到数据列表控件的值的源。

数据源必须是实现System.Collections.IEnumerable接口(例如System.Data.DataView、system.Collections.ArrayList或System.Collections.Hashtable)

               或IListSource接口的对象。(如:dataset,datatable,dataview)

1、List<T>数据源

  

List
List<string> li=new List<string>();
For(int i=0;i<=10;i++)
{
      li.add(i.tostring());  
}
xxxx.DataSource=li;
xxxx.DataBind();

 

List<类>
自定义一个类
public class MyClass
{
    public MyClass()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    private string _Name;
    private string _Home;
    public string Name
    {
        set { _Name = value; }
        get { return _Name; }
    }
    public string Home
    {
        set { _Home = value; }
        get { return _Home; }
    }
}


List<MyClass> li=new List<MyClass>();
For(int i=0;i<=10;i++)
{
      MyClass cl=new MyClass();
      cl.Name=i+"Name";
      cl.Home=i+"Home";
      li.Add(cl);
}
DropDownList1.DataSource=li;
DropDownList1.DataTextField="Name";
DropDownList1.DataBind();

 

 

posted @ 2013-03-10 23:44  武沛齐  阅读(418)  评论(0编辑  收藏  举报