This article describes how to nest the Repeater control into the DataGrid to display hierarchical data. Of course, you can also apply this technology to other list binding controls, such as a combination of DataGrid and DataList.
The complete code is as follows. Note that,
DataGrid1.DataSource = dv;
DataGrid1.DataBind ();
It must be after ds. Relations. Add ..
<% @ Import namespace = "System. Data" %>
<% @ Import namespace = "System. Data. OleDb" %>
<Script language = "c #" runat = "server">
String connstr = ConfigurationSettings. etettings ["ConnectionString"];
// Community information
String SQL = "select * from location where location_id in (select location from catalyst )";
// Switch Information
String sqltext = "select * from catalyst order by location desc ";
Private void page_load (Object obj, EventArgs e)
{
If (! Page. IsPostBack)
{
Dataload ();
}
}
Private void Dataload ()
{
Try
{
OleDbConnection conn = new OleDbConnection (connstr );
DataSet ds = new DataSet ();
OleDbDataAdapter da = new OleDbDataAdapter (SQL, conn );
Da. Fill (ds, "location ");
DataView dv = new DataView (ds. Tables ["location"]);
OleDbDataAdapter da2 = new OleDbDataAdapter (sqltext, conn );
Da2.Fill (ds, "catalyst ");
Ds. relations. add ("myrelation", ds. tables ["location"]. columns ["location_id"], ds. tables ["catalyst"]. columns ["location"]);
DataGrid1.DataSource = dv;
DataGrid1.DataBind ();
Conn. Close ();
}
Catch (OleDbException ex)
{
Info. Text = ex. Message;
}
}
</Script>
<Html>
<Body>
<Form runat = server>
<Asp: DataGrid id = "DataGrid1" runat = "server" AutoGenerateColumns = "false">
<Columns>
<Asp: TemplateColumn HeaderText = "cell name">
<ItemTemplate>
<% # DataBinder. Eval (Container. DataItem, "location_name") %>
</ItemTemplate>
</Asp: TemplateColumn>
<Asp: TemplateColumn HeaderText = "vswitch No.">
<ItemTemplate>
<Asp: Repeater id = "ChildRepeater" DataSource = '<% # (DataRowView) Container. dataItem ). row. getChildRows ("myrelation") %> 'runat = "server">
<ItemTemplate>
<% # Container. ItemIndex + 1%>. <% # DataBinder. Eval (Container. DataItem, "[\" label \ "]") %> <br>
</ItemTemplate>
</Asp: Repeater>
</ItemTemplate>
</Asp: TemplateColumn>
</Columns>
</Asp: DataGrid>
</Form>
<Asp: label id = "info" forecolor = "blue" runat = "server"/>
</Body>
</Html>