Gridview Date Format Problem

Discussion in 'ASP .Net Datagrid Control' started by Jules Wensley, Dec 17, 2005.

  1. Hi

    I have a Gridview in ASP.Net 2.0. I'm populating it with data from a SQL
    2005 database (but with legacy design).
    In the DB, the dates are stored as DateTime - DD/MM/YY HH:MM:SS.

    In the bound Gridview, I'm applying the {0:d} format (to show just the short
    date).

    The date is still displayed in the same format as the DB.

    Anybody know of a solution to this, or am I going to populate the Gridview
    manually, applying formatting then.

    Thanks in advance for your assistance.

    Jules
     
    Jules Wensley, Dec 17, 2005
    #1
    1. Advertising

  2. Jules,

    My information is based on 1.1 but it's likely still valid for 2.0.

    0:d is for numbers. You can't format date in such a way. You should catch
    ItemDataBound or PreRender event, get the string content of the cell,
    convert it to DateTime and than back to string with a valid date format
    expression.

    Eliyahu

    "Jules Wensley" <> wrote in message
    news:...
    >
    > Hi
    >
    > I have a Gridview in ASP.Net 2.0. I'm populating it with data from a SQL
    > 2005 database (but with legacy design).
    > In the DB, the dates are stored as DateTime - DD/MM/YY HH:MM:SS.
    >
    > In the bound Gridview, I'm applying the {0:d} format (to show just the
    > short date).
    >
    > The date is still displayed in the same format as the DB.
    >
    > Anybody know of a solution to this, or am I going to populate the Gridview
    > manually, applying formatting then.
    >
    > Thanks in advance for your assistance.
    >
    > Jules
    >
    >
     
    Eliyahu Goldin, Dec 18, 2005
    #2
    1. Advertising

  3. Jules Wensley

    Guest

    , Dec 19, 2005
    #3
  4. I have idem problem.
    But my quikly solution is

    Into event:
    gvFacturas_RowDataBound....
    {...actions....}
    Select Case e.Row.RowType
    {...actions....}
    Case DataControlRowType.DataRow
    {...actions....}
    Dim Fecha As Date =
    CType(DataBinder.Eval(e.Row.DataItem, "FacturaFecha"), Date)
    e.Row.Cells(2).Text =
    Fecha.ToShortDateString

    I hope it works..!
     
    Jose A. Fernandez, Dec 19, 2005
    #4
  5. Jules Wensley

    Teemu Keiski Guest

    Hi,

    you mean string formatting doesn't seem to apply when using BoundField?

    Set HtmlEncode="false" for the BoundField. That should do it.

    For an explanation please see:
    http://aspadvice.com/blogs/joteke/archive/2005/09/25/12871.aspx

    --
    Teemu Keiski
    ASP.NET MVP, AspInsider
    Finland, EU
    http://blogs.aspadvice.com/joteke



    "Jules Wensley" <> wrote in message
    news:...
    >
    > Hi
    >
    > I have a Gridview in ASP.Net 2.0. I'm populating it with data from a SQL
    > 2005 database (but with legacy design).
    > In the DB, the dates are stored as DateTime - DD/MM/YY HH:MM:SS.
    >
    > In the bound Gridview, I'm applying the {0:d} format (to show just the
    > short date).
    >
    > The date is still displayed in the same format as the DB.
    >
    > Anybody know of a solution to this, or am I going to populate the Gridview
    > manually, applying formatting then.
    >
    > Thanks in advance for your assistance.
    >
    > Jules
    >
    >
     
    Teemu Keiski, Dec 20, 2005
    #5
  6. Thanks very much, all. In the end, I just amended the SQL string. We're going
    to SQL Server 2005 next week, so we'll be able to do it properly!

    Jules
    Hi,
    >
    > you mean string formatting doesn't seem to apply when using
    > BoundField?
    >
    > Set HtmlEncode="false" for the BoundField. That should do it.
    >
    > For an explanation please see:
    > http://aspadvice.com/blogs/joteke/archive/2005/09/25/12871.aspx
    > "Jules Wensley" <> wrote in message
    > news:...
    >
    >> Hi
    >>
    >> I have a Gridview in ASP.Net 2.0. I'm populating it with data from a
    >> SQL
    >> 2005 database (but with legacy design).
    >> In the DB, the dates are stored as DateTime - DD/MM/YY HH:MM:SS.
    >> In the bound Gridview, I'm applying the {0:d} format (to show just
    >> the short date).
    >>
    >> The date is still displayed in the same format as the DB.
    >>
    >> Anybody know of a solution to this, or am I going to populate the
    >> Gridview manually, applying formatting then.
    >>
    >> Thanks in advance for your assistance.
    >>
    >> Jules
    >>
     
    Jules Wensley, Dec 20, 2005
    #6
  7. Jules Wensley

    Guest

    Follow Teemu's advice then your formatting will work!

    This seems to be a bug to me, because everyone asks this question why
    the formatting isn't working.
     
    , Dec 20, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian Candy
    Replies:
    2
    Views:
    1,210
    Janaka
    Feb 18, 2004
  2. DC
    Replies:
    5
    Views:
    3,038
    Brendan Reynolds
    Jul 1, 2005
  3. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,380
    Michael Borgwardt
    May 30, 2004
  4. Bob Phillips

    GridView date format problem

    Bob Phillips, Jul 27, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    271
    Bob Phillips
    Aug 1, 2006
  5. Une bévue

    problem with date and date/format

    Une bévue, Mar 10, 2006, in forum: Ruby
    Replies:
    3
    Views:
    121
    Une bévue
    Mar 12, 2006
Loading...

Share This Page