如何防止从数据表中删除毫秒 [英] How to prevent removing miliseconds from DataTable
问题描述
您好开发者
我正在通过在DataGridView中使用并将其可视化将一个DataTable值导入到第二个DataTable.
Hello developers
I am importing one DataTable values to second DataTable by using and visualizing them in DataGridView.
foreach (DataRow dr in datatable.Rows)
{
datatable2.ImportRow(dr);
}
这段代码运行良好,但问题是我的第二个DataTable包含一个FIRST_DATE列,其值类似于02-06-09 12:09:25.
但是,当我将第一个DataTable值导入第二个DataTable值时,它们的显示方式如下02.06.09 12:09 毫秒数已从所有FIRST_DATA值中删除.
请解决我的问题.
[注意:被删除的时间是秒,而不是毫秒(反正用双l拼写)]
This code is working good but a problem is that my second DataTable contains a FIRST_DATE column whose values are like 02-06-09 12:09:25.
But, when I import first DataTable values into second DataTable values they are viewed like this 02.06.09 12:09 miliseconds are removed from all FIRST_DATA values.
Please solve my problem.
[Note: those removed are seconds, not milliseconds (spelled with double l, anyway)]
推荐答案
Hallo Vishu,
导入行时,我无法验证精度损失.我做了一个快速测试:
Hallo Vishu,
I can not verify loss of precision when importing rows. I did a quick test:
using System;
using System.Data;
namespace Removed_Seconds
{
class Program
{
static void Main(string[] args)
{
DateTime datetime = DateTime.Now;
DataTable tableOriginal = new DataTable("Original");
tableOriginal.Columns.Add("Time", typeof(DateTime));
DataTable tableCopy = new DataTable("Copy");
tableCopy.Columns.Add("Time", typeof(DateTime));
tableOriginal.Rows.Add(datetime);
tableCopy.ImportRow(tableOriginal.Rows[0]);
DateTime dtOriginal = (DateTime) tableOriginal.Rows[0]["Time"];
DateTime dtCopy = (DateTime) tableCopy.Rows[0]["Time"];
Console.WriteLine("Original Time: {0}:{1}", dtOriginal, dtOriginal.Millisecond);
Console.WriteLine("Copy Time: {0}:{1}", dtCopy, dtCopy.Millisecond);
Console.ReadKey();
}
}
}
结果:值将按预期方式导入.
看来您还有其他问题.
顺便提一句.您讨论了毫秒,但是在您的示例中,您表明秒丢失了
(02.06.09 12:09)
Result: Values are imported as expected.
It seems you have some other problem.
Btw. You talked about Milliseconds but in your example you showed that Seconds are missing
(02.06.09 12:09)
这篇关于如何防止从数据表中删除毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!