//示例代码 DataTable ndt = new DataTable(); ndt.Columns.AddRange(new DataColumn[] { new DataColumn("Typename", typeof(string)), new DataColumn("count", typeof(int)), new DataColumn("nowCount", typeof(int)) }); DataTable dtResult = ds.Tables[0].Clone(); DataTable dtName = ds.Tables[0].DefaultView.ToTable(true, "Typename"); for (int i = 0; i < dtName.Rows.Count; i++) { DataRow[] rows = ds.Tables[0].Select("Typename='" + dtName.Rows[i][0] + "'"); //temp用来存储筛选出来的数据 DataTable temp = dtResult.Clone(); foreach (DataRow row in rows) { temp.Rows.Add(row.ItemArray); } DataRow dr = ndt.NewRow(); dr[0] = dtName.Rows[i][0].ToString(); dr[1] = temp.Compute("count(Typename)", ""); dr[2] = 0; ndt.Rows.Add(dr); }
备注:
ds.Tables[0] //需要分组的数据源table