简体   繁体   English

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

[英]How to change column format in DataTable C#

In my application returns following datatable,在我的应用程序返回以下数据表,

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

In above datatable , WaitTime and AssistTime data coming as double value,Now I need to change the format of WaitTime and AssistTime columns format to 00:00:00 (hh:mm:ss) format.另外,在上述datatableWaitTimeAssistTime数据来作为双精度值,现在,我需要的格式改变WaitTimeAssistTime列格式00:00:00 (hh:mm:ss)的格式。 So I just write folowing code(please be noted this part of code).所以我只写了以下代码(请注意这部分代码)。

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

But above code gives this error,但是上面的代码给出了这个错误,

System.ArgumentException: 'Input string was not in a correct format.Couldn't store <00:00:18> in WaitTime Column. System.ArgumentException: '输入字符串的格式不正确。无法在 WaitTime 列中存储 <00:00:18>。 Expected type is Decimal.'预期类型为十进制。 FormatException: Input string was not in a correct format. FormatException: 输入字符串的格式不正确。

I need following DataTable as formated one.我需要按照格式化的 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 |
+-------------+----------+------------+-------+-------+----------+

How can I do this?我怎样才能做到这一点? please help请帮忙

As Jeff mentioned in his answer, You cannot change the DataType after the Datatable is filled with data.正如杰夫在他的回答中提到的那样,在数据表填充数据后,您无法更改数据类型。 What you can do is, take a clone of the Data table, change the column type and load data from the original data table to the cloned table as follows.您可以做的是,克隆数据表,更改列类型并将数据从原始数据表加载到克隆表中,如下所示。

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

Then you can use your code as,然后你可以使用你的代码,

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

you are successfully changing values from column WaitTime and AssistTime您正在成功更改 WaitTime 和 AssistTime 列中的值

now just follow the below steps现在只需按照以下步骤操作

        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;

The type of your WaitTime column is a decimal so you cannot set it to a TimeSpan. WaitTime 列的类型是小数,因此您不能将其设置为 TimeSpan。 You cannot change the column types of DataTables after they have been populated so you'll have to change it at the source or create a clone.在填充 DataTables 后,您无法更改它们的列类型,因此您必须在源中更改它或创建一个克隆。

See this answer https://stackoverflow.com/a/9028087/1532710请参阅此答案https://stackoverflow.com/a/9028087/1532710

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM