IList怎么和DataGrid绑定


  <form   runat="server">  
    <asp:DataGrid   id="DataGrid1"   runat="server"   />  
  <BR>  
    <asp:DataGrid   id="DataGrid2"   runat="server"   AutoGenerateColumns="false">  
        <Columns>  
  <asp:TemplateColumn   HeaderText="value">  
  <ItemTemplate>  
  <%#   DataBinder.Eval(Container.DataItem,"MyProp")   %>  
  </ItemTemplate>  
  </asp:TemplateColumn>  
        </Columns>  
    </asp:DataGrid>  
   
    <asp:Button   id="btn"   runat="server"   Text="Refresh"   />  
  </form>  
   
  <script   language="C#"   runat="server">  
  class   MyClass  
  {  
      string   ms;  
      public   MyClass(string   s)  
      {  
  ms   =s;  
      }  
   
      public   string   MyProp  
      {  
  get   {return   ms;}  
      }  
  }  
   
  void   Page_Load(Object   sender,   EventArgs   e)  
  {  
    if   (!IsPostBack)  
    {  
      ArrayList   al   =   new   ArrayList();  
      al.Add(new   MyClass("a"));  
      al.Add(new   MyClass("b"));  
      DataGrid1.DataSource   =   (IList)al;  
      DataGrid1.DataBind();  
   
          DataGrid2.DataSource   =   (IList)al;  
      DataGrid2.DataBind();  
    }  
  }  
  </script> 

 

资料2 :

来源:   http://www.codes-bbs.cn/html/ASP-NET/2006/10/43486.html

我把代码整理一下:  
   
  public   class   RowData  
  {  
  public   string   product_id;  
  public   string   product_nm;  
  public   string   Product_id  
  {  
  get{return   product_id;}  
  set{product_id   =   value;}  
  }  
  public   string   Product_nm  
  {  
  get{return   product_nm;}  
  set{product_nm   =   value;}  
  }  
   
  public   RowData(string   id,string   des)  
  {  
  this.Product_id=id;  
  this.Product_nm=des;  
  }  
  }  
   
  class   RowDataCollection:CollectionBase  
  {  
  public   RowData   this[   int   index   ]      
  {  
  get      
  {  
  return(   (RowData)   List[index]   );  
  }  
  set      
  {  
  List[index]   =   value;  
  }  
  }  
   
  public   int   Add(   RowData   value   )      
  {  
  return(   List.Add(   value   )   );  
  }  
   
  public   int   IndexOf(   RowData   value   )      
  {  
  return(   List.IndexOf(   value   )   );  
  }  
   
  public   void   Insert(   int   index,   RowData   value   )      
  {  
  List.Insert(   index,   value   );  
  }  
   
  public   void   Remove(   RowData   value   )      
  {  
  List.Remove(   value   );  
  }  
   
  public   bool   Contains(   RowData   value   )      
  {  
  //   If   value   is   not   of   type   RowData,   this   will   return   false.  
  return(   List.Contains(   value   )   );  
  }  
   
  protected   override   void   OnInsert(   int   index,   Object   value   )      
  {  
  if   (   value.GetType()   !=   Type.GetType("WebApplication1.RowData")   )  
  throw   new   ArgumentException(   "value   must   be   of   type   RowData.",   "value"   );  
  }  
   
  protected   override   void   OnRemove(   int   index,   Object   value   )      
  {  
  if   (   value.GetType()   !=   Type.GetType("WebApplication1.RowData")   )  
  throw   new   ArgumentException(   "value   must   be   of   type   RowData.",   "value"   );  
  }  
   
  protected   override   void   OnSet(   int   index,   Object   oldValue,   Object   newValue   )      
  {  
  if   (   newValue.GetType()   !=   Type.GetType("WebApplication1.RowData")   )  
  throw   new   ArgumentException(   "newValue   must   be   of   type   RowData.",   "newValue"   );  
  }  
   
  protected   override   void   OnValidate(   Object   value   )      
  {  
  if   (   value.GetType()   !=   Type.GetType("WebApplication1.RowData")   )  
  throw   new   ArgumentException(   "value   must   be   of   type   RowData."   );  
  }  
   
  }  
   
  class   Test  
  {  
  private   RowDataCollection   m_RowDataCollection;  
  public   RowDataCollection   RowCollection  
  {  
  set{m_RowDataCollection=value;}  
  get{return   m_RowDataCollection;}  
  }  
   
  public   void   LoadData()  
  {  
  this.RowCollection=new   RowDataCollection();  
  this.RowCollection.Add(new   RowData("7201","aaaaaa"));  
  this.RowCollection.Add(new   RowData("7202","aaaaaa"));  
  this.RowCollection.Add(new   RowData("7203","aaaaaa"));  
  this.RowCollection.Add(new   RowData("7204","aaaaaa"));  
  }  
  }  
   
  private   void   Page_Load(object   sender,   System.EventArgs   e)  
  {  
  //   在此处放置用户代码以初始化页面  
  Test   t=new   Test();  
  t.LoadData();  
  RowData   rd=t.RowCollection[1];  
  this.DataGrid1.DataSource=t.RowCollection;  
  this.DataGrid1.DataBind();  
  }  
   
   
   
  <asp:DataGrid   id="DataGrid1"   runat="server"   Width="529px"   Height="208px"   AutoGenerateColumns="False">  
  <Columns>  
  <asp:BoundColumn   HeaderText="a1"   DataField="product_id"></asp:BoundColumn>  
  <asp:BoundColumn   HeaderText="a2"   DataField="product_nm"></asp:BoundColumn>  
  </Columns>  
  </asp:DataGrid>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值