作为一个问题,如何对输出排序以在dataTable中显示升序。我想按数据表中的start_time对其进行排序。我是C#的新手。
我已经像这样将每个变量添加到dataTable中
dt.Rows.Add(product_name, first_Column, third_Column, run_time, inspected, pass, reject, invalid, yield, start_time, stop_time);
这是我显示dataTable中数据的地方。但是,我如何获得日期呢?
foreach(DataRow row1 in dt.Rows)
{
//string product = string.Format("{0}",row1.ItemArray[0]); //row1.ItemArray[0];
var productName = row1.ItemArray[0];
var firstColumn = row1.ItemArray[1];
var thirdColumn = row1.ItemArray[2];
var runTime = row1.ItemArray[3];
var Inspected = row1.ItemArray[4];
var Pass = row1.ItemArray[5];
var Reject = row1.ItemArray[6];
var Invalid = row1.ItemArray[7];
var Yield = row1.ItemArray[8];
var startTime = row1.ItemArray[9];
var stopTime = row1.ItemArray[10];
//if (startTime != null || startTime < startDateTemp)
//string startDateTemp = startTime.ToLongDateString();
Console.WriteLine();
Console.WriteLine(string.Format("|{0,5}|{1,5}|{2,5}|{3,5}|{4,5}|{5,5}|{6,5}|{7,5}|{8,5}|{9,5}|{10,5}|"
, productName, firstColumn, thirdColumn, runTime, Inspected, Pass, Reject, Invalid, Yield, startTime, stopTime));
//DateTime maxDate = Convert.ToDateTime(((from DataRow dr in dt.Rows orderby Convert.ToDateTime(dr[9]) descending select dr).FirstOrDefault()[9]));
//Console.WriteLine(maxDate);
}
答案 0 :(得分:0)
您可以使用DataTable的Select(字符串表达式,字符串顺序)方法代替DataTable.Rows。那是
foreach(DataRow row1 in dt.Select('','start_time ASC'))
{
...
}