网上有很多说的,试过,都没用。自己研究了一下,解决方案如下:
建立dataview,用dv.rowfilter,就可以取到了,然后TOTABLE即可。
代码如下:(只看中间那几句即可)
1 private void CreatTable(DataSet dsAll) 2 { 3 DataTable dtXH = new DataTable(); 4 dtXH.Columns.AddRange(new DataColumn[] { new DataColumn("xh_id", typeof(System.String)), new DataColumn("a", typeof(System.String)) }); 5 DataTable dtGfr = new DataTable(); 6 dtGfr.Columns.AddRange(new DataColumn[] { new DataColumn("xh_id", typeof(System.String)), new DataColumn("gfr", typeof(System.String)), new DataColumn("gfrsbh", typeof(System.String)) }); 7 8 if (dsAll.Tables.Count > 0) 9 { 10 DataTable dtitem = dsAll.Tables["item"]; 11 DataRow[] dritems = dtitem.Select("xh_id<>''"); 12 foreach (DataRow dr in dritems) 13 { 14 DataRow drXH = dtXH.NewRow(); 15 drXH["xh_id"] = dr["xh_id"]; 16 drXH["a"] = dr["a"]; 17 dtXH.Rows.Add(drXH); 18 } 19 DataTable dtChild = dsAll.Tables["aChild"]; 20 //解决方案:建立dataview,用dv.rowfilter,就可以取到了 21 DataView dv = dtitem.DefaultView; 22 foreach (DataRow dr in dtChild.Rows) 23 { 24 int itemid = Int32.Parse(dr["item_id"].ToString()); 25 int aChildid = Int32.Parse(dr["aChild_id"].ToString()); 26 string xh_id = ""; 27 28 dv.RowFilter = string.Format("item_Id = {0}", itemid); 29 DataTable dttt = dv.ToTable(); 30 //这么是查不到的 31 //DataRow[] drxhid = dtitem.Select(dv.RowFilter); 32 //if (drxhid.Length > 0) 33 //{ 34 // xh_id = ((DataRow)drxhid[0])["xh_id"].ToString(); 35 //} 36 if (dttt.Rows.Count > 0) 37 { 38 xh_id = dttt.Rows[0]["xh_id"].ToString(); 39 } 40 DataRow[] drChilds = dtitem.Select("aChild_id='" + aChildid + "'"); 41 foreach (DataRow drChild in drChilds) 42 { 43 DataRow drgfr = dtGfr.NewRow(); 44 drgfr["xh_id"] = xh_id; 45 drgfr["gfr"] = drChild["gfr"].ToString(); 46 drgfr["gfrsbh"] = drChild["gfrsbh"].ToString(); 47 dtGfr.Rows.Add(drgfr); 48 } 49 } 50 } 51 }
希望对大家有所帮助!