<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>