[英]Converting values from long to TimeSpan in data grid view (C# with SQL Server)
I'm having a problem concerning my school project. 我有关于我的学校项目的问题。 I have a SQL Server table where I save flight duration (expressed in minutes), saved in ticks (in a
bigint
column). 我有一个SQL Server表,其中保存飞行时间(以分钟表示),以刻度线(在
bigint
列中)保存。
In the program I have a datagridview where I load all the data from the SQL Server table and I'd now need to convert the "flight duration" column from ticks to time (something like HH:mm
format), but I have no idea how would I achieve this. 在程序中,我有一个datagridview,在其中加载SQL Server表中的所有数据,现在需要将“飞行时间”列从滴答转换为时间(类似于
HH:mm
格式),但是我不知道我将如何实现这一目标。
My current code for converting the column looks like this, but the third line gives me error about converting 'object' to 'long'. 我当前用于转换列的代码看起来像这样,但是第三行给我关于将'object'转换为'long'的错误。
for (int i = 0; i < dgvTabla.RowCount; i++)
{
var dt = new DateTime(dgvTabla[5, i].Value);
string.Format("HH:mm", dt);
dgvTabla[5, i].Value = dt;
}
Any ideas? 有任何想法吗? Thanks!
谢谢!
If you want to convert your DURATION on the SQL Server side 如果要在SQL Server端转换DURATION
Example 例
Declare @YourTable table (ID int,duration bigint)
Insert Into @YourTable values
(1,600)
,(2,1365)
Select A.*
,AsTime = cast(dateadd(MINUTE,duration,0) as time)
,AsString = format(dateadd(MINUTE,duration,0),'HH:mm')
From @YourTable A
Returns 退货
ID duration AsTime AsString
1 600 10:00:00.0000000 10:00
2 1365 22:45:00.0000000 22:45
You can call from Sql Server and display it directly to Grid. 您可以从Sql Server调用并将其直接显示到Grid。 That will be easier to to do it.
这样做会更容易。 Below is the example
以下是示例
DECLARE @Time INT = 90
SELECT substring(CONVERT(char(8), DATEADD(MINUTE, @Time, ''), 108),0,6)
Hope that helps. 希望能有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.