A. McLean
A. McLean

Reputation: 55

Hide time value from date type column when editing in GridView

I've got a GridView that displays dates as "dd-mm-yyyy" which is what I want, but when editing I get "dd-mm-yyyy 00:00:00". Since I am using a date data type, I don't want the time included when editing since my validation for date will only accept "dd/mm/yyyy". Am I using the wrong data type or do I need to change this line?

<asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("GameDate") %>'></asp:TextBox>

This GridView is taking data from a database.

Upvotes: 1

Views: 568

Answers (3)

fnostro
fnostro

Reputation: 4591

I assume the GridView field is a template Field? If so give this a try:

  <asp:TemplateField HeaderText="Game Date" SortExpression="GameDate">
    <EditItemTemplate>
      <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("GameDate","{0:MM-dd-yyyy}") %>' ID="TextBox1"></asp:TextBox>
    </EditItemTemplate>
    <ItemTemplate>
      <asp:Label runat="server" Text='<%# Bind("GameDate", "{0:MM-dd-yyyy}") %>' ID="Label1"></asp:Label>
    </ItemTemplate>
  </asp:TemplateField>

Upvotes: 1

techspider
techspider

Reputation: 3410

Use formatting

<%# (DateTime.Parse(Eval("GameDate").ToString()).ToString("MM/dd/yyyy")) %>

Upvotes: 1

Akshey Bhat
Akshey Bhat

Reputation: 8545

Convert(varchar(30),GameDate,103) as GameDate

Use convert function in database select query. It will remove time.

Upvotes: 0

Related Questions