另外一種DataGrid與Datalist的嵌套--更好的方式
來源:互聯網
上載者:User
datagrid
相對於上一次的使用Relation的缺點:
1. 不能限制返回的紀錄數;2. 邦定時不方便,甚至不能邦定上;3. 程式簡單,但不容易理解。4. 可能對於三層以上的嵌套不容易實現。
這次給出的嵌套方案,使用的ItemDataBound事件進行嵌套,實現起來很方便,其容易理解。並且解決了以上四個問題。
以下是主要的代碼:
<asp:DataList ID="DList_Class" DataKeyField="NavigatorID"
BackColor="#ffffff" BorderWidth="0px" CellPadding="1"
ShowFooter="False" ShowHeader="False" runat="server"
CellSpacing="20" HorizontalAlign="Justify" RepeatColumns="2"
RepeatDirection="Horizontal" RepeatLayout="Table" >
<itemstyle VerticalAlign=Top></itemstyle>
<itemtemplate>
<b><%# DataBinder.Eval(Container.DataItem,"NavigatorID") %> @
<%# DataBinder.Eval(Container.DataItem,"NodeCaption") %></b>
<hr>
<asp:DataGrid ID=DG_SubClass Width=315px Runat=server
AutoGenerateColumns="false"
ShowHeader="false" ShowFooter="false" PageSize="10" AllowPaging="true"
PagerStyle-Visible="false" BorderWidth="1" BackColor="#ffffff" BorderColor="#ffffff"
CellPadding="0" CellSpacing="0" GridLines=Horizontal
OnItemDataBound='DG_SubClass_ItemDataBound'>
<ItemStyle Height="14px" BackColor="#e3f1ff" CssClass="gridHover"></ItemStyle>
<Columns>
<asp:BoundColumn DataField ="ModuleType" ReadOnly=True >
<ItemStyle Width=25 HorizontalAlign=Center ></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="NodeCaption" >
<ItemStyle HorizontalAlign=Left CssClass="nav"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField ="ModuleType" ReadOnly=True>
<ItemStyle Width=90 HorizontalAlign=Center></ItemStyle>
</asp:BoundColumn>
</Columns>
</asp:DataGrid>
</itemtemplate>
</asp:DataList>
private void DList_Class_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataGrid dg=(DataGrid)e.Item.FindControl("DG_SubClass");
string itemIndex =this.DList_Class.DataKeys[e.Item.ItemIndex].ToString();
string[] strSqlArray =new string[3]{"select Top 10 IOrder, ILevel, IParentID, NavigatorID, NodeCaption,ModuleID, ModuleType from NavigatorNodes where IParentID in ( ", itemIndex, ");"};
string strSql=string.Concat(strSqlArray);
string TableName ="SubClass"+itemIndex;
this.dbClass.AdapterFill(ds, CommandType.Text, strSql, TableName, new OleDbParameter[0]);
dg.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DG_SubClass_ItemDataBound);
dg.DataSource =ds.Tables[TableName].DefaultView;
dg.DataBind();
}
}
====================
更多內容,歡迎訪問ms.mblogger.cn/nono