Date Formating

Discussion in 'ASP .Net Datagrid Control' started by Seli, Mar 4, 2004.

  1. Seli

    Seli Guest

    Hello

    I would like to format a date in the DataGrid to be 3.3.2004 and not with the time (default?) 3.3.2004 00:00:00!

    <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 264px
    runat="server"> AutoGenerateColumns="True" </asp:DataGrid

    DataGrid1.DataSource=ds.Tables["Products"].DefaultView
    DataGrid1.DataBind()

    DataGrid1.Columns[0].DataFormatString = "{0:d}"

    The last line does not work

    Thank
    Seli
     
    Seli, Mar 4, 2004
    #1
    1. Advertising

  2. Seli wrote:
    > I would like to format a date in the DataGrid to be 3.3.2004 and not with the time (default?) 3.3.2004 00:00:00!!
    >
    > <asp:DataGrid id="DataGrid1" style="Z-INDEX: 102; LEFT: 80px; POSITION: absolute; TOP: 264px"
    > runat="server"> AutoGenerateColumns="True" </asp:DataGrid>
    >
    > DataGrid1.DataSource=ds.Tables["Products"].DefaultView;
    > DataGrid1.DataBind();
    >
    > DataGrid1.Columns[0].DataFormatString = "{0:d}";


    Two things:

    1.) You need to set the DataFormatString *before* the call to DataBind()
    2.) The Columns[0] returns a DataGridColumn class instance. This class
    doesn't have a DataFormatString. The BoundColumn class does. So you
    need to cast Columns[0] to a BoundColumn before trying to muddle with
    the DataFormatString property. Something like:

    ((BoundColumn) DataGrid1.Columns[0]).DataFormatString = "{0:d}";

    Remember to place this **before** the DataBind() call...

    Happy Programming!

    --

    Scott Mitchell

    http://www.4GuysFromRolla.com
    http://www.ASPFAQs.com
    http://www.ASPMessageboard.com

    * When you think ASP, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Mar 4, 2004
    #2
    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. John Carnahan

    Help on date formating

    John Carnahan, Nov 19, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    315
    Alvin Bruney
    Nov 19, 2003
  2. Russ
    Replies:
    2
    Views:
    289
  3. Fabiano

    Date Formating

    Fabiano, Jan 14, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    346
    André Almeida Maldonado
    Jan 14, 2004
  4. JAPHET

    Data Grid Date Formating

    JAPHET, Nov 2, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    464
    Scott Allen
    Nov 3, 2004
  5. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,285
    Michael Borgwardt
    May 30, 2004
Loading...

Share This Page