Use Repeater nested in the DataGrid

Source: Internet
Author: User
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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.