日期:2014-05-18 浏览次数:21385 次
System.Data.DataTable dataTable1 = new System.Data.DataTable("BlogUser"); System.Data.DataRow dr; dataTable1.Columns.Add(new System.Data.DataColumn("批号", typeof(System.String))); dataTable1.Columns.Add(new System.Data.DataColumn("数量", typeof(System.Int32))); dataTable1.Rows.Add(new Object[] { "0001", 100 }); dataTable1.Rows.Add(new Object[] { "0002", 200 }); dataTable1.Rows.Add(new Object[] { "0001", 300 }); dataTable1.Rows.Add(new Object[] { "0002", 400 }); dataTable1.Rows.Add(new Object[] { "0003", 500 }); System.Data.DataView dv = dataTable1.DefaultView; System.Data.DataTable dataTable2 = dv.ToTable(true, "批号"); String s = ""; for (int i = 0; i < dataTable2.Rows.Count; i++) { String type = dataTable2.Rows[i][0].ToString(); s += type + "=" + dataTable1.Compute("Sum(数量)", "批号='" + type + "'").ToString() + Environment.NewLine; } MessageBox.Show(s);