GridView DataFormatString问题。将Doudle转换为TimeStamp

时间:2012-03-29 11:17:51

标签: c# asp.net

在GridView中需要一些DataFormatString的帮助。我有一个需要显示为TimeSpan的Double值。我试过DataFormatString =“{0:HH:mm:ss}”。这没用。

在C#中测试了一下,我会这样做:

TimeSpan.FromHours(16.7358217592592).ToString()

这给了我。

"16:44:08.9580000"

我追求的是什么。但是如何在ASP.Net中获取它是一个大问题。 有什么建议吗?

我有一个看起来像这样的简单GridView。

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1">
<Columns>
    <asp:BoundField DataField="sOprTime" HeaderText="Sum OprTime" ReadOnly="True" SortExpression="sOprTime" />
    <asp:BoundField DataField="sWorkTime" HeaderText="Sum WorkTime" ReadOnly="True" SortExpression="sWorkTime" />
    <asp:BoundField DataField="sFaultTime" HeaderText="Sum FaultTime" ReadOnly="True" SortExpression="sFaultTime" />
</Columns>
<EmptyDataTemplate>
    No data is present.
</EmptyDataTemplate>

SQL查询如下所示:

sqlString1 = string.Format(@"SELECT SUM(CONVERT (Float, OprTime)) AS sOprTime, SUM(CONVERT (Float, WorkTime)) AS sWorkTime, SUM(CONVERT (Float, OprTime)) - SUM(CONVERT (Float, WorkTime)) AS sFaultTime FROM tblNovikLogg WHERE (Date_Time > '{0:yyyy/MM/dd H:mm:ss}' And Date_Time < '{1:yyyy/MM/dd H:mm:ss}')",  selectedDate, selectedDate.AddDays(1));

1 个答案:

答案 0 :(得分:1)

您可以在RowDataBound中执行此操作:

protected void Grid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        var row = ((DataRowView)e.Row.DataItem).Row;
        Double sWorkTime = row.Field<Double>("sWorkTime");
        TimeSpan ts = TimeSpan.FromHours(sWorkTime);
        String formattedTimespan = String.Format(@"{0:hh\:mm\:ss}", ts);
        e.Row.Cells[1].Text = formattedTimespan;
    }
}