ArrayList--Gridview--datasource

  1. //定义一些实体   
  2.         public class Entity  
  3.         {       
  4.             public Entity(string Names,string Sex)  
  5.             {  
  6.                 this.Names = Names;  
  7.                 this.Sex = Sex;  
  8.             }  
  9.             public string Names { get; set; }  
  10.             public string Sex { get; set; }  
  11.         }  
  12.         //定义一些String   
  13.         public class SomeStr  
  14.         {  
  15.             public string Names;  
  16.             public string Sex;  
  17.             public SomeStr(string Names,string Sex)  
  18.             {  
  19.                 this.Names = Names;  
  20.                 this.Sex = Sex;  
  21.             }  
  22.         }  
  23.         //ArrayList实体绑定GridView   
  24.         private void AL()  
  25.         {  
  26.             ArrayList ai = new ArrayList();  
  27.             ai.Add(new Entity("Dean", "Male"));  
  28.             ai.Add(new Entity("Cass", "Male"));  
  29.             GridView1.DataSource = ai;  
  30.             GridView1.DataBind();  
  31.         }  
  32.         //ArrayList字符绑定GridView   
  33.         private void AlStr()  
  34.         {  
  35.             ArrayList ai = new ArrayList();  
  36.             ai.Add(new SomeStr("Dean", "Male"));  
  37.   
  38.             DataTable dt = new DataTable();  
  39.             //手动添加列名   
  40.             dt.Columns.Add("Names", System.Type.GetType("System.String"));  
  41.             dt.Columns.Add("Sex", System.Type.GetType("System.String"));  
  42.               
  43.             //从ArrayList读出每一个枚举   
  44.             IEnumerator ie = ai.GetEnumerator();  
  45.             while (ie.MoveNext())  
  46.             {  
  47.                 //转换一下类型   
  48.                 SomeStr sr = (SomeStr)ie.Current;  
  49.                 DataRow dr = dt.NewRow();  
  50.                 //给列赋值   
  51.                 dr["Names"] = sr.Names;  
  52.                 dr["Sex"] = sr.Sex;  
  53.                 dt.Rows.Add(dr);  
  54.             }  
  55.             GridView1.DataSource = dt;  
  56.             GridView1.DataBind();  
  57.   
  58.         }  

posted on 2012-03-13 21:37 青蛙學堂 阅读(1027) 评论(1)  编辑 收藏 引用 所属分类: 軟件布袋數據庫

评论

# re: ArrayList--Gridview--datasource 2012-03-13 21:39 dede

ArrayList myList = new ArrayList();//创建ArranList while (dr.Read()) { Info myinfo = new Info();//创建类Info的实例 myinfo.Name = dr["Name"].ToString(); myinfo.VoteCount = Int32.Parse(dr["VoteCount"].ToString()); myList.Add(myinfo);//添加Arranlist } dr.Close(); mygridView.DataSource = myList;//arrayList绑定到Gridview mygridView.DataBind();  回复  更多评论   

只有注册用户登录后才能发表评论。
<2012年3月>
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

统计

常用链接

留言簿(8)

随笔分类

随笔档案

收藏夹

青蛙学堂

最新评论

阅读排行榜

评论排行榜