本文描述了如何把Repeater 控件嵌套进DataGrid来显示分级的数据 当然你也可以将这一技术应用到其他的列表绑定控件上去比如DataGrid包含DataGridDataList包含DataList等等的组合 完整的代码 如下要注意的是 DataGridDataSource=dv; DataGridDataBind(); 一定要在dsRelationsAdd()之后 <%@import namespace=SystemData %> <%@import namespace=SystemDataOleDb%> <script language=c# runat=server> string connstr=ConfigurationSettingsAppSettings[ConnectionString]; //小区信息 string sql=select * from location where location_id in(select location from catalyst); //交换机信息 string sqltext=select * from catalyst order by location desc; private void page_load(Object objEventArgs e) { if(!PageIsPostBack) { Dataload(); } } private void Dataload() { try { OleDbConnection conn=new OleDbConnection(connstr); DataSet ds=new DataSet(); OleDbDataAdapter da=new OleDbDataAdapter(sqlconn); daFill(dslocation); DataView dv=new DataView(dsTables[location]); OleDbDataAdapter da=new OleDbDataAdapter(sqltextconn); daFill(dscatalyst); dsRelationsAdd(myrelationdsTables[location]Columns[location_id]dsTables[catalyst]Columns[location]); DataGridDataSource=dv; DataGridDataBind(); connClose(); } catch(OleDbException ex) { infoText=exMessage; } } </script> <html> <body> <form runat=server> <asp:DataGrid id=DataGrid runat=server AutoGenerateColumns=false> <Columns> <asp:TemplateColumn HeaderText=小区名> <ItemTemplate> <%# DataBinderEval(ContainerDataItemlocation_name) %> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText=交换机编号> <ItemTemplate> <asp:Repeater id=ChildRepeater DataSource=<%# ((DataRowView)ContainerDataItem)RowGetChildRows(myrelation) %> runat=server> <ItemTemplate> <%# ContainerItemIndex+%><%# DataBinderEval(ContainerDataItem [\label\])%><br> </ItemTemplate> </asp:Repeater> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> </form> <asp:label id=info forecolor=blue runat=server/> </body> </html> |