DataGrid中嵌套使用Repeater

本文描述了如何把Repeater 控件嵌套进DataGrid来显示分级的数据 。当然,你也可以将这一技术应用到其他的列表绑定控件上去,比如DataGrid包含DataGrid,DataList包含DataList等等的组合。

完整的代码 如下,要注意的是,

DataGrid1.DataSource=dv;
DataGrid1.DataBind();

一定要在ds.Relations.Add(..)之后..

< %@import namespace="System.Data" %>
< %@import namespace="System.Data.OleDb"%>

 1<script language="c#" runat="server">   
 2string connstr=ConfigurationSettings.AppSettings["ConnectionString"]; 
 3
 4//小区信息   
 5string sql="select * from location where location_id in(select location from catalyst)";   
 6//交换机信息   
 7string sqltext="select * from catalyst order by location desc"; 
 8
 9private void page_load(Object obj,EventArgs e)   
10{   
11if(!Page.IsPostBack)   
12{   
13Dataload();   
14}   
15} 
16
17private void Dataload()   
18{   
19try   
20{   
21OleDbConnection conn=new OleDbConnection(connstr);   
22DataSet ds=new DataSet();   
23OleDbDataAdapter da=new OleDbDataAdapter(sql,conn);   
24da.Fill(ds,"location");   
25DataView dv=new DataView(ds.Tables["location"]); 
26
27OleDbDataAdapter da2=new OleDbDataAdapter(sqltext,conn);   
28da2.Fill(ds,"catalyst");   
29ds.Relations.Add("myrelation",ds.Tables["location"].Columns["location_id"],ds.Tables["catalyst"].Columns["location"]); 
30
31DataGrid1.DataSource=dv;   
32DataGrid1.DataBind(); 
33
34conn.Close();   
35}   
36catch(OleDbException ex)   
37{   
38info.Text=ex.Message;   
39}   
40}   
41</script>
1<html>
2<body>
3<form runat="server">
4<asp:datagrid autogeneratecolumns="false" id="DataGrid1" runat="server">
5<columns>
6<asp:templatecolumn headertext="小区名">
7<itemtemplate>   

DataBinder.Eval(Container.DataItem,"location_name")

1</itemtemplate>
2</asp:templatecolumn>
3<asp:templatecolumn headertext="交换机编号">
4<itemtemplate>
5<asp:repeater datasource='```
6# ((DataRowView)Container.DataItem).Row.GetChildRows("myrelation") 
7```' id="ChildRepeater" runat="server">
8<itemtemplate>   

Container.ItemIndex+1

.

DataBinder.Eval(Container.DataItem, "["label"]")

 1</itemtemplate>
 2</asp:repeater>
 3</itemtemplate>
 4</asp:templatecolumn>
 5</columns>
 6</asp:datagrid>
 7</form>
 8<asp:label forecolor="blue" id="info" runat="server"></asp:label>
 9</body>
10</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus