我有一个绑定到 time(0) SQL 数据字段的 GridView 控件。我想使用 DataFormatString 属性将输出呈现为 AM/PM(例如下午 1:35)。我尝试分配几个不同的值,但编译器抛出“输入字符串格式不正确”。大多数情况下都会出错。到目前为止,我已经尝试了以下值:
{0:hh:mm tt}
{0:hh:mm:ss tt}
{0:hh:mm t}
{0:hh:mm:ss t}
{0:t} - 符合但不转换格式
{0:tt} - 符合但不转换格式
<asp:GridView SkinID="ResultsGrid" ID="GridViewAlerts" style="margin-top:2%"
runat="server" DataKeyNames="ID" AutoGenerateColumns="False" OnPageIndexChanging="GridViewAlerts_PageIndexChanging"
OnSorting="GridViewAlerts_Sorting" OnRowCommand="GridViewAlerts_RowCommand"
OnDataBound="GridViewAlerts_DataBound">
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="10%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="10%" />
</asp:BoundField>
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="30%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="30%" />
</asp:BoundField>
<asp:BoundField DataField="Interval" HeaderText="Interval" SortExpression="Interval" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Run_At" HeaderText="At" SortExpression="Run_At" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" DataFormatString="{0:t}" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Last_Run_Dt" HeaderText="Last Run" SortExpression="Last_Run_Dt" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" DataFormatString="{0: MM/dd/yyyy}" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Next_Run_Dt" HeaderText="Next Run" SortExpression="Next_Run_Dt" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" DataFormatString="{0: MM/dd/yyyy}" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Last_Result" HeaderText="Result" SortExpression="Last_Result" ItemStyle-HorizontalAlign="Left"
HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%" >
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Active" HeaderText="Status" SortExpression="Active"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner"
ItemStyle-HorizontalAlign="Left" HeaderStyle-HorizontalAlign="Left" ItemStyle-Width="5%">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" Width="5%" />
</asp:BoundField>
<asp:CommandField ButtonType="Image" SelectImageUrl="~/Images/gear.png" HeaderText="Settings" ShowSelectButton="true" ShowHeader="True" ItemStyle-Width="5%" />
<asp:TemplateField HeaderText="Start/Stop" ItemStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton runat="server" ID="ImageButtonStartStop" ToolTip="Start or stop this alert" ImageUrl="~/Images/start.png"
OnClientClick="GridViewAlerts_RowCommand" CommandName="StartStop" CausesValidation="false"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Run" ItemStyle-Width="5%">
<ItemTemplate>
<asp:ImageButton runat="server" ID="ImageButtonRun" ToolTip="Manually run this alert" ImageUrl="~/Images/play.png"
OnClientClick="GridViewAlerts_RowCommand" CommandName="Run" CausesValidation="false"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
![screenshot][1]