如何更改DataTable C#中的列格式

How to change column format in DataTable C#

在我的应用程序中 returns 以下数据表,

+-------------+----------+------------+-------+-------+----------+
| AccountName | WaitTime | AssistTime | FName | LName | FullName |
+-------------+----------+------------+-------+-------+----------+
| XXX         | 18       | 15         | Mary  | Sil   |          |
+-------------+----------+------------+-------+-------+----------+
| YYY         | 67       | 3          | Jany  | Joh   |          |
+-------------+----------+------------+-------+-------+----------+
| ZZZ         | 50       | 100        | Kate  | Ham   |          |
+-------------+----------+------------+-------+-------+----------+

上面的datatableWaitTimeAssistTime数据是double值,现在我需要改变WaitTimeAssistTime列的格式格式为 00:00:00 (hh:mm:ss) 格式。所以我只写了下面的代码(请注意这部分代码)。

DataTable tableone = ds.Tables[0];

tableone.Select().ToList().ForEach(row =>
{

    string FirstName = Convert.ToString(row["FName"], CultureInfo.InvariantCulture);
    string LastName = Convert.ToString(row["LName"], CultureInfo.InvariantCulture);
    double xxx = Convert.ToDouble(row["WaitTime"]);

    row.SetField("WaitTime",secondsToTime(xxx));
    row.SetField("FullName", string.Format("{0} {1}", FirstName, LastName));
});

private string secondsToTime(double seconds)
{
    TimeSpan t = TimeSpan.FromSeconds(seconds);
    string answer = string.Format("{0:D2}:{1:D2}:{2:D2}",
        t.Hours,
        t.Minutes,
        t.Seconds);
    return answer;
}

但是上面的代码给出了这个错误,

System.ArgumentException: 'Input string was not in a correct format.Couldn't store <00:00:18> in WaitTime Column. Expected type is Decimal.' FormatException: Input string was not in a correct format.

我需要按照格式化后的数据表进行操作。

+-------------+----------+------------+-------+-------+----------+
| AccountName | WaitTime | AssistTime | FName | LName | FullName |
+-------------+----------+------------+-------+-------+----------+
| XXX         | 00:00:18 | 00:00:15   | Mary  | Sil   | Mary Sil |
+-------------+----------+------------+-------+-------+----------+
| YYY         | 00:01:07 | 00:00:03   | Jany  | Joh   | Jany Joh |
+-------------+----------+------------+-------+-------+----------+
| ZZZ         | 00:00:50 | 00:01:40   | Kate  | Ham   | Kate Ham |
+-------------+----------+------------+-------+-------+----------+

我该怎么做?请帮忙

您的 WaitTime 列的类型是小数,因此您不能将其设置为 TimeSpan。 DataTables 的列类型在填充后无法更改,因此您必须在源中更改它或创建一个克隆。

看到这个答案

您正在成功更改 WaitTime 和 AssistTime 列的值

现在只需按照以下步骤操作

        DataTable dtTemp = new DataTable();

        dtTemp = dtOri.Clone();
        dtTemp.Columns["WaitTime"].DataType = typeof(TimeSpan);
        dtTemp.Columns["AssistTime"].DataType = typeof(TimeSpan);
        //you can change data type to string as well if you need
        //if you are changing datatype to string make sure to add ".ToString()" in below code e.g secondsToTime(xx).ToString()

        foreach (DataRow row in dtOri.Rows)
        {
            dtTemp.Rows.Add(new object[] {row[0], secondsToTime(Convert.ToDouble(row[1].ToString())), secondsToTime(Convert.ToDouble(row[2].ToString())), row[3],row[4],row[5]});
        }

        dtOri = dtTemp;

正如 Jeff 在他的回答中提到的,在 Datatable 填充数据后,您无法更改 DataType。您可以做的是,克隆数据 table,更改列类型并将数据从原始数据 table 加载到克隆的 table,如下所示。

DataTable dtCloned = tableone.Clone();
dtCloned.Columns[1].DataType = typeof(string); //In your case you need to change WaitTime and AssistTime
dtCloned.Columns[2].DataType = typeof(string);

foreach (DataRow row in tableone.Rows)
{
    dtCloned.ImportRow(row);
}

然后您可以将您的代码用作,

dtCloned.Select().ToList().ForEach(row =>
{
    double xxx = Convert.ToDouble(row["WaitTime"]);
    row.SetField("WaitTime", secondsToTime(xxx));
});