C# 有五个var zz(包含sn,orderid,recordid,creationdate),var gn,var lh,var bz,var ck,把这五个里的数据存到一个datatable dt 此dt列为 Recordid,orderid,sn,zztesttime,gntesttime,gnresult,pvresult,batresult,bztime,cktime 并显示到dgv
时间: 2023-09-03 17:06:01 浏览: 178
好的,我来提供一下代码:
```csharp
// 创建DataTable并添加列
DataTable dt = new DataTable();
dt.Columns.Add("Recordid", typeof(int));
dt.Columns.Add("orderid", typeof(string));
dt.Columns.Add("sn", typeof(string));
dt.Columns.Add("zztesttime", typeof(DateTime));
dt.Columns.Add("gntesttime", typeof(DateTime));
dt.Columns.Add("gnresult", typeof(string));
dt.Columns.Add("pvresult", typeof(string));
dt.Columns.Add("batresult", typeof(string));
dt.Columns.Add("bztime", typeof(DateTime));
dt.Columns.Add("cktime", typeof(DateTime));
// 添加zz数据
object[] zzDataRow = new object[4];
zzDataRow[0] = 1; // Recordid
zzDataRow[1] = "order001"; // orderid
zzDataRow[2] = "sn001"; // sn
zzDataRow[3] = DateTime.Now; // creationdate
dt.Rows.Add(zzDataRow);
// 添加gn数据
object[] gnDataRow = new object[3];
gnDataRow[0] = 1; // Recordid
gnDataRow[1] = "order001"; // orderid
gnDataRow[2] = "gnresult001"; // gnresult
dt.Rows.Add(gnDataRow);
// 添加lh数据
object[] lhDataRow = new object[3];
lhDataRow[0] = 1; // Recordid
lhDataRow[1] = "order001"; // orderid
lhDataRow[2] = "lhresult001"; // lhresult
dt.Rows.Add(lhDataRow);
// 添加bz数据
object[] bzDataRow = new object[2];
bzDataRow[0] = 1; // Recordid
bzDataRow[1] = DateTime.Now; // bztime
dt.Rows.Add(bzDataRow);
// 添加ck数据
object[] ckDataRow = new object[2];
ckDataRow[0] = 1; // Recordid
ckDataRow[1] = DateTime.Now; // cktime
dt.Rows.Add(ckDataRow);
// 进行关联
var query = from r in dt.AsEnumerable()
group r by r.Field<int>("Recordid") into g
select new
{
Recordid = g.Key,
Orderid = g.First().Field<string>("orderid"),
Sn = g.First().Field<string>("sn"),
ZzTestTime = g.First().Field<DateTime>("creationdate"),
GnTestTime = g.FirstOrDefault(r => r.Field<string>("gnresult") != null)?.Field<DateTime>("creationdate"),
GnResult = g.FirstOrDefault(r => r.Field<string>("gnresult") != null)?.Field<string>("gnresult"),
PvResult = g.FirstOrDefault(r => r.Field<string>("pvresult") != null)?.Field<string>("pvresult"),
BatResult = g.FirstOrDefault(r => r.Field<string>("batresult") != null)?.Field<string>("batresult"),
BzTime = g.FirstOrDefault(r => r.Field<DateTime?>("bztime") != null)?.Field<DateTime>("bztime"),
CkTime = g.FirstOrDefault(r => r.Field<DateTime?>("cktime") != null)?.Field<DateTime>("cktime")
};
DataTable resultDt = new DataTable();
resultDt.Columns.Add("Recordid", typeof(int));
resultDt.Columns.Add("orderid", typeof(string));
resultDt.Columns.Add("sn", typeof(string));
resultDt.Columns.Add("zztesttime", typeof(DateTime));
resultDt.Columns.Add("gntesttime", typeof(DateTime));
resultDt.Columns.Add("gnresult", typeof(string));
resultDt.Columns.Add("pvresult", typeof(string));
resultDt.Columns.Add("batresult", typeof(string));
resultDt.Columns.Add("bztime", typeof(DateTime));
resultDt.Columns.Add("cktime", typeof(DateTime));
foreach (var row in query)
{
DataRow newRow = resultDt.NewRow();
newRow["Recordid"] = row.Recordid;
newRow["orderid"] = row.Orderid;
newRow["sn"] = row.Sn;
newRow["zztesttime"] = row.ZzTestTime;
newRow["gntesttime"] = row.GnTestTime;
newRow["gnresult"] = row.GnResult;
newRow["pvresult"] = row.PvResult;
newRow["batresult"] = row.BatResult;
newRow["bztime"] = row.BzTime;
newRow["cktime"] = row.CkTime;
resultDt.Rows.Add(newRow);
}
// 显示到DataGridView
dgv.DataSource = resultDt;
```
这段代码会创建一个DataTable对象,然后添加10个列,每个列的数据类型不同。然后,我们分别将zz、gn、lh、bz、ck的数据添加到DataTable中。接着,我们将这些数据进行关联,将它们转换成我们需要的结果,并存储到一个新的DataTable中。最后,将新的DataTable绑定到DataGridView上,实现显示效果。
阅读全文