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

问题描述 投票:1回答:3

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

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

datatable以上,WaitTimeAssistTime数据以双值形式出现,现在我需要将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:'输入字符串不正确格式。无法在WaitTime列中存储<00:00:18>。预期类型为小数。”FormatException:输入字符串的格式不正确。

我需要遵循DataTable的格式。

+-------------+----------+------------+-------+-------+----------+
| 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 |
+-------------+----------+------------+-------+-------+----------+

我该怎么做?请帮助

c# .net datatable format timespan
3个回答
2
投票

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

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));
});

1
投票

您正在成功更改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;

0
投票

WaitTime列的类型为小数,因此无法将其设置为TimeSpan。填充数据表后,您不能再更改它们的列类型,因此您必须在源头进行更改或创建克隆。

查看此答案https://stackoverflow.com/a/9028087/1532710

© www.soinside.com 2019 - 2024. All rights reserved.