使用自定義的數(shù)據(jù)源進行DataGrid控件的數(shù)據(jù)綁定
2024-07-21 02:23:31
供稿:網(wǎng)友
注冊會員,創(chuàng)建你的web開發(fā)資料庫,自定義的集合類
/// <summary>
/// collection 的摘要說明。
/// </summary>
public class collection : system.collections.collectionbase
{
public collection()
{
for(int i = 0;i < 10;i++)
{
base.innerlist.add(new element(i,string.format("a[{0}]",i)));
}
}
}
集合元素類
public class element
{
private string name;
public string valuename
{
get{return name;}
}
private int valu;
public int value
{
get{return valu;}
}
public element(int val,string nam)
{
name = nam;
valu = val;
}
}
aspx的后置代碼
/// <summary>
/// webform1 的摘要說明。
/// </summary>
public class webform1 : system.web.ui.page
{
protected system.web.ui.webcontrols.datagrid datagrid1;
private void page_load(object sender, system.eventargs e)
{
datagrid1.datasource = new collection();
datagrid1.databind();
}
#region web 窗體設計器生成的代碼
override protected void oninit(eventargs e)
{
//
// codegen: 該調(diào)用是 asp.net web 窗體設計器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void initializecomponent()
{
this.load += new system.eventhandler(this.page_load);
}
#endregion
}
aspx頁的html代碼
<body ms_positioning="flowlayout">
<form id="form1" method="post" runat="server">
<asp:datagrid id="datagrid1" runat="server" autogeneratecolumns="false" width="224px">
<columns>
<asp:templatecolumn headertext="名稱">
<itemtemplate>
<asp:label runat="server" text='<%# databinder.eval(container, "dataitem.valuename") %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat="server" text='<%# databinder.eval(container, "dataitem") %>'>
</asp:textbox>
</edititemtemplate>
</asp:templatecolumn>
<asp:templatecolumn headertext="數(shù)字">
<itemtemplate>
<asp:label runat="server" text='<%# databinder.eval(container, "dataitem.value") %>'>
</asp:label>
</itemtemplate>
<edititemtemplate>
<asp:textbox runat="server" text='<%# databinder.eval(container, "dataitem") %>'>
</asp:textbox>
</edititemtemplate>
</asp:templatecolumn>
</columns>
</asp:datagrid>
</form>
</body>
</html>