Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Time format in GridView (http://www.velocityreviews.com/forums/t695487-time-format-in-gridview.html)

David C 08-20-2009 06:57 PM

Time format in GridView
 
I have a Label bound to a Time(0) column data type and want it to display
hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have the
Label as below but it displays 09:00:00 and wondered if my format was wrong.
I have used this on a datetime column before and it worked great. Thanks.

David

<asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
"{0:hh:mm tt}") %>'></asp:Label>



Stan 08-21-2009 02:39 PM

Re: Time format in GridView
 
On 20 Aug, 19:57, "David C" <dlch...@lifetimeinc.com> wrote:
> I have a Label bound to a Time(0) column data type and want it to display
> hh:mm tt *(e.g. 09:15 AM) when the Time column contains 09:00:00 *I have the
> Label as below but it displays 09:00:00 and wondered if my format was wrong.
> I have used this on a datetime column before and it worked great. *Thanks.
>
> David
>
> <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
> "{0:hh:mm tt}") %>'></asp:Label>


You have to use a template field for DateTime formatting. For some
reason it doesn't work for Bound fields.

David C 08-21-2009 03:22 PM

Re: Time format in GridView
 

"Stan" <googlestan@philhall.net> wrote in message
news:b432e805-4cb4-4290-8a22-75fccd6782c2@34g2000yqi.googlegroups.com...
On 20 Aug, 19:57, "David C" <dlch...@lifetimeinc.com> wrote:
> I have a Label bound to a Time(0) column data type and want it to display
> hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have the
> Label as below but it displays 09:00:00 and wondered if my format was
> wrong.
> I have used this on a datetime column before and it worked great. Thanks.
>
> David
>
> <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
> "{0:hh:mm tt}") %>'></asp:Label>


You have to use a template field for DateTime formatting. For some
reason it doesn't work for Bound fields.

It is in a Template. Below is the section of GridView.

<asp:TemplateField HeaderText="My To Do"
SortExpression="ToDo">
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<table>
<tr>
<td colspan="3">
<asp:LinkButton ID="LBtnToDo"
runat="server" Text='<%# Bind("ToDo") %>' OnClientClick="opentasks(0);return
false;"></asp:LinkButton>
</td>
</tr>
<tr>
<td>By:</td>
<td><asp:Label ID="LblToDoDate"
runat="server" Text='<%# Bind("ToDoDate", "{0:M/d/yy}")
%>'></asp:Label></td>
<td><asp:Label ID="LblToDoTime"
runat="server" Text='<%# Bind("ToDoTime", "{0:hh:mm tt}")
%>'></asp:Label></td>
</tr>
</table>

</ItemTemplate>
</asp:TemplateField>



David C 08-21-2009 03:35 PM

Re: Time format in GridView
 
"Stan" <googlestan@philhall.net> wrote in message
news:b432e805-4cb4-4290-8a22-75fccd6782c2@34g2000yqi.googlegroups.com...
On 20 Aug, 19:57, "David C" <dlch...@lifetimeinc.com> wrote:
> I have a Label bound to a Time(0) column data type and want it to display
> hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have the
> Label as below but it displays 09:00:00 and wondered if my format was
> wrong.
> I have used this on a datetime column before and it worked great. Thanks.
>
> David
>
> <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
> "{0:hh:mm tt}") %>'></asp:Label>


You have to use a template field for DateTime formatting. For some
reason it doesn't work for Bound fields.

Also, it worked when ToDoTime was a DateTime column on SQL Server but now it
is a Time(0) field on SQL Server 2008. Hope this helps.

David



David C 08-21-2009 04:12 PM

Re: Time format in GridView
 

"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message
news:erutecnIKHA.6068@TK2MSFTNGP03.phx.gbl...
> "David C" <dlchase@lifetimeinc.com> wrote in message
> news:eAOu5icIKHA.4708@TK2MSFTNGP03.phx.gbl...
>
>>I have a Label bound to a Time(0) column data type and want it to display
>>hh:mm tt (e.g. 09:15 AM) when the Time column contains 09:00:00 I have
>>the Label as below but it displays 09:00:00 and wondered if my format was
>>wrong. I have used this on a datetime column before and it worked great.
>>Thanks.
>>
>> David
>>
>> <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
>> "{0:hh:mm tt}") %>'></asp:Label>

>
> You need to add HtmlEncode="false" e.g.
>
> <asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
> "{0:hh:mm tt}") %>' HtmlEncode="false" />
>
> Ignore the reply about TemplateFields...
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


Mark,
That didn't work either. I changed the page code to below but 2:00 PM still
displays as 14:00:00

David

<td><asp:Label ID="LblToDoTime" runat="server" Text='<%# Bind("ToDoTime",
"{0:hh:mm tt}") %>' HtmlEncode="False"></asp:Label></td>



David C 08-22-2009 10:31 PM

Re: Time format in GridView
 

"Mark Rae [MVP]" <mark@markNOSPAMrae.net> wrote in message
news:eyqt%23DwIKHA.4004@TK2MSFTNGP05.phx.gbl...
> "David C" <dlchase@lifetimeinc.com> wrote in message
> news:eVsCgrnIKHA.1252@TK2MSFTNGP04.phx.gbl...
>
>> That didn't work either. I changed the page code to below but 2:00 PM
>> still displays as 14:00:00

>
>
> We've been here before, haven't we...?
> http://www.microsoft.com/communities...&cr=&sloc=&p=1
>
>
> --
> Mark Rae
> ASP.NET MVP
> http://www.markrae.net


Yes, that is why I am confused. The only difference is that the one I am
currently having trouble with is in a Master Page and is in a TemplateField
with a table. See full GridView below.

<asp:GridView ID="gvTasks" runat="server"
AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid"
BorderWidth="1px"
CellPadding="3" DataKeyNames="TaskID"
DataSourceID="SqlTasks" ForeColor="Black"
GridLines="Vertical" EmptyDataText="No Current Tasks"
AllowPaging="True"
PageSize="7" Width="100%">
<EmptyDataRowStyle BackColor="Blue" ForeColor="White"
Width="150px" HorizontalAlign="Center" />
<Columns>
<asp:BoundField DataField="TaskID" HeaderText="TaskID"
ReadOnly="True"
SortExpression="TaskID" Visible="False" />
<asp:TemplateField HeaderText="My To Do"
SortExpression="ToDo">
<EditItemTemplate>
</EditItemTemplate>
<ItemTemplate>
<table>
<tr>
<td colspan="3">
<asp:LinkButton ID="LBtnToDo"
runat="server" Text='<%# Bind("ToDo") %>' OnClientClick="opentasks(0);return
false;"></asp:LinkButton>
<asp:TextBox ID="txtPersonID"
runat="server" Text='<%# Bind("PersonID") %>' CssClass="Hide"></asp:TextBox>
</td>
</tr>
<tr>
<td>By:</td>
<td><asp:Label ID="LblToDoDate"
runat="server" Text='<%# Bind("ToDoDate", "{0:M/d/yy}")
%>'></asp:Label></td>
<td><asp:Label ID="LblToDoTime"
runat="server" Text='<%# Bind("ToDoTime", "{0:hh:mm tt}") %>'
HtmlEncode="false"></asp:Label></td>
</tr>
</table>

</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<PagerStyle BackColor="#999999" ForeColor="Black"
HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True"
ForeColor="White" />
<HeaderStyle BackColor="Black" Font-Bold="True"
ForeColor="White"
Font-Size="Medium" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>



pallavi_a 10-28-2012 09:16 PM

Hi ,

I am facing a similar problem. I have a textbox bound to a Time(0) column data type in sql server 2008 and want it to display
hh:mm tt (e.g. 09:00 AM) when the Time column contains 09:00:00

<asp:TextBox ID="TimeInTextBox_Item" runat="server" Text='<%# Bind("TimeIn","{0:hh\:mm tt}") %>' CssClass="textboxFont" Width="70px" >
</asp:TextBox>

But it throws error when the above format is given. I also tried with hh:mm tt but it still throws error.

Let me know if someone can help me with that. Thanks


All times are GMT. The time now is 01:25 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.