C# 如何更改DataTable C中的列格式#
在我的应用程序中,返回以下数据表C# 如何更改DataTable C中的列格式#,c#,.net,datatable,format,timespan,C#,.net,Datatable,Format,Timespan,在我的应用程序中,返回以下数据表 +-------------+----------+------------+-------+-------+----------+ | AccountName | WaitTime | AssistTime | FName | LName | FullName | +-------------+----------+------------+-------+-------+----------+ | XXX | 18 | 15
+-------------+----------+------------+-------+-------+----------+
| AccountName | WaitTime | AssistTime | FName | LName | FullName |
+-------------+----------+------------+-------+-------+----------+
| XXX | 18 | 15 | Mary | Sil | |
+-------------+----------+------------+-------+-------+----------+
| YYY | 67 | 3 | Jany | Joh | |
+-------------+----------+------------+-------+-------+----------+
| ZZZ | 50 | 100 | Kate | Ham | |
+-------------+----------+------------+-------+-------+----------+
在上面的datatable
,WaitTime
和AssistTime
数据作为双值出现时,现在我需要将WaitTime
和AssistTime
列格式更改为00:00(hh:mm:ss)
格式。所以我只写以下代码(请注意这部分代码)
但是上面的代码给出了这个错误
System.ArgumentException:'输入字符串的格式不正确
格式。无法存储在WaitTime列中。预期类型为
十进位。”
FormatException:输入字符串的格式不正确
我需要以下数据表作为格式化表
+-------------+----------+------------+-------+-------+----------+
| 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。数据表的列类型在填充后无法更改,因此必须在源位置进行更改或创建克隆 看到这个答案了吗
WaitTime列的类型为十进制,因此无法将其设置为时间跨度。数据表的列类型在填充后无法更改,因此必须在源位置进行更改或创建克隆 看到这个答案了吗
您正在成功更改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;
您正在成功更改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中填充数据后,您不能更改数据类型。您可以做的是,克隆数据表,更改列类型,并将数据从原始数据表加载到克隆表,如下所示
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));
});
正如Jeff在回答中提到的,在Datatable中填充数据后,您不能更改数据类型。您可以做的是,克隆数据表,更改列类型,并将数据从原始数据表加载到克隆表,如下所示
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));
});
数据库中的时间是什么类型的?它是数字还是日期对象?看起来您的代码需要一个整数[row.SetField(“WaitTime”,secondsToTime(xxx));]。@jdweng
WaitTime
和AssistTime
是数据库中的十进制值。您能告诉我您的代码[row.SetField(“WaitTime”,secondsToTime(xxx));]
与我的代码有什么不同吗:)数据库中的时间是什么类型的?它是数字还是日期对象?看起来您的代码需要一个整数[row.SetField(“WaitTime”,secondsToTime(xxx));]。@jdwengWaitTime
和AssistTime
是数据库中的十进制值。您能告诉我您的代码[row.SetField(“WaitTime”,secondsToTime(xxx));]
和我的代码有什么区别吗:)您能告诉我,我如何使用克隆数据表来实现这一点,请帮助我。实际上我不需要设定时间跨度secondsToTime
函数返回字符串值。对不起,我不明白你说的克隆数据库是什么意思。DataTable是数据的.Net内存表示形式。您可以使用设计器在代码中手动定义一个,也可以允许框架从数据源自动为您创建一个。其他答案显示了通过克隆数据表并更改类型来自定义数据表的一种方法。您还可以更改根源(api服务或数据库)的列类型。请告诉我,如何使用clone datatable进行更改,请帮助我。实际上我不需要设定时间跨度secondsToTime
函数返回字符串值。对不起,我不明白你说的克隆数据库是什么意思。DataTable是数据的.Net内存表示形式。您可以使用设计器在代码中手动定义一个,也可以允许框架从数据源自动为您创建一个。其他答案显示了通过克隆数据表并更改类型来自定义数据表的一种方法。您还可以在根源(api服务或数据库)更改列类型。