Professional Web Applications Themes

Date Formating - ASP.NET Data Grid Control

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...

  1. #1

    Default Date Formating

    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 Guest

  2. #2

    Default Re: Date Formating

    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
    [email]mitchell4guysfromrolla.com[/email]
    [url]http://www.4GuysFromRolla.com[/url]
    [url]http://www.ASPFAQs.com[/url]
    [url]http://www.ASPMessageboard.com[/url]

    * When you think ASP, think 4GuysFromRolla.com!
    Scott Mitchell [MVP] Guest

Similar Threads

  1. Formating Date
    By LJG in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 3
    Last Post: August 22nd, 05:02 PM
  2. MX formating date differently than 5 with Oracle
    By flesher_rob in forum Coldfusion Database Access
    Replies: 4
    Last Post: December 21st, 04:02 AM
  3. Date formating in DataGrid
    By Maged in forum Macromedia Flex General Discussion
    Replies: 2
    Last Post: November 24th, 02:00 AM
  4. [PHP] formating date
    By Peter Knight in forum PHP Development
    Replies: 0
    Last Post: July 30th, 04:07 PM
  5. Formating date,time
    By Deuvenx in forum Macromedia Dreamweaver
    Replies: 0
    Last Post: July 26th, 03:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139