第一步:声明
1 DataTable recordsToShow = new DataTable(); 2 recordsToShow.Columns.Add("ResultID", typeof(string)); 3 recordsToShow.Columns.Add("Username", typeof(string)); 4 recordsToShow.Columns.Add("RealName", typeof(string)); 5 recordsToShow.Columns.Add("CompanyName", typeof(string)); 6 recordsToShow.Columns.Add("BTime", typeof(string)); 7 recordsToShow.Columns.Add("ETime", typeof(string)); 8 recordsToShow.Columns.Add("Score", typeof(string)); 9 recordsToShow.Columns.Add("IP", typeof(string));
第二步:给DataTable 赋值
examResults用于循环赋值的数组
1 foreach (var result in examResults) 2 { 3 var row = recordsToShow.NewRow(); 4 row.SetField("Username", result.Username); 5 row.SetField("ResultID", result.Result_ID); 6 row.SetField("RealName", NetTrmp.Common.BLL.People.GetPeopleNameByUsername(result.Username)); 7 row.SetField("CompanyName", NetTrmp.Common.BLL.People.GetCompanyNameByUsername(result.Username, true)); 8 row.SetField("BTime", result.BTime.HasValue ? result.BTime.Value.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty); 9 row.SetField("ETime", result.ETime.HasValue ? result.ETime.Value.ToString("yyyy-MM-dd HH:mm:ss") : string.Empty); 10 row.SetField("Score", (result.Score * result.Rate).Value.ToString(numberformatstring)); 11 row.SetField("IP", result.IP);21 22 recordsToShow.Rows.Add(row); 23 }