Problem:
Datalist1 shows the data for this table say
Then you want to nest a Datalist2 in Datalist1 to display the corresponding comment
Table 2 Sayid corresponds to the ID of table 1, if the corresponding Sayid is not found in table 2, Datalist2 is not displayed in the corresponding Datalist1.
The difficulty lies in how to achieve "Lin Xia" said below only show "Huang Lei", "propelled letter" comments, "Han" below Only show "Chengcheng" comments. (The Lin Xia ID is 3, so the query corresponds to the row Sayid 3 in table 2).
Code
Foreground code:
Background cs:
protected void Page_Load (object sender, EventArgs e) {if (! Page.IsPostBack) {this.
Datalist1.datasource = Data (); This.
Datalist1.databind ();
} public DataSet Data () {DataSet ds = new DataSet ();
DataTable dtparent = new DataTable ();
DTPARENT.COLUMNS.ADD ("id", typeof (int));
DTPARENT.COLUMNS.ADD ("username", typeof (String));
DTPARENT.COLUMNS.ADD ("text", typeof (String));
DTPARENT.COLUMNS.ADD ("Date", typeof (String));
DTPARENT.COLUMNS.ADD ("Place", typeof (String));
DTPARENT.ROWS.ADD (1, "Morgan", "This ...", "2014.6", "London");
DTPARENT.ROWS.ADD (2, "Han Cold", "trend ...", "2015.7", "Hunan");
DTPARENT.ROWS.ADD (3, "Lin Xia", "Years ...", "2016.2", "small Town");
DTPARENT.ROWS.ADD (4, "Faye Wong", "I Will ...", "2015.9", "Hong Kong");
DTPARENT.ROWS.ADD (5, "Guo", "We ...", "2015.1", "Shanghai");DataTable dtchild = new DataTable ();
DTCHILD.COLUMNS.ADD ("id", typeof (int));
DTCHILD.COLUMNS.ADD ("SayId", typeof (int));
DTCHILD.COLUMNS.ADD ("username", typeof (String));
DTCHILD.COLUMNS.ADD ("text", typeof (String));
DTCHILD.COLUMNS.ADD ("Date", typeof (String));
DTCHILD.ROWS.ADD (1, 3, "Huang Lei", "I was the first to comment", "2016.6.17 13:06:12");
DTCHILD.ROWS.ADD (3, 3, "propelled letter", "comment on Lin Xia's saying", "2016.6.17 12:26:43");
DTCHILD.ROWS.ADD (4, 2, "Chengcheng", "I comment Han Cold", "2015.3.1 15:20:10"); Ds.
Tables.addrange (new datatable[] {dtparent, dtchild});
DataRelation dr = new DataRelation ("Relation", dtparent.columns["id"], dtchild.columns["sayId"]); Ds.
Relations.Add (DR); Dtparent = ds. Relations["Relation"].
parenttable; Dtchild = ds. Relations["Relation"].
childtable;
return DS;
} protected void Datalist1_itemdatabound (object sender, DataListItemEventArgs e) {if (E.item.itemtype = = ListItemType.Item | | e.item.itemtype = = listitemtype.alternatingitem)
{DataList DataList = E.item.findcontrol ("DataList2") as DataList;
DataRowView ROWV = (DataRowView) e.Item.DataItem; datarow[] rows = Data (). TABLES[1]. Select ("sayid= '" + rowv["id"].
ToString () + "'"); DataTable dt = Data (). TABLES[1].
Clone (); foreach (DataRow dr in rows) {dt.
ImportRow (DR);
} datalist.datasource = dt;
Datalist.databind (); }
}
The data source part of the background code can be replaced with the Ado.net query database method.
Effect Chart: