dataformatstring template column why is getting a short date so ha

Discussion in 'ASP .Net Datagrid Control' started by cindy, Apr 19, 2006.

  1. cindy

    cindy Guest

    I have datagrid, datasource datafield is datetime so its
    ugly date time etc ugly I want short date
    in datagrid column is datetime datatype so it sorts right
    not like text so thats good but its ugly

    have tried this but the error is invalid cast
    <asp:TemplateColumn SortExpression="reviseddate" HeaderText="Revised Date">
    <ItemTemplate><asp:Label runat="server" Text='<%#
    ((DateTime)DataBinder.Eval(Container.DataItem,
    "reviseddate")).ToShortDateString() %>' ID="Label2"></asp:Label>
    </ItemTemplate>

    have tried this but then the grid just does not display it binds and then
    cannot display

    the code below was auto created, as a bound column the use of
    dataformatstring
    works great then using property builder in design mode and convert to
    template
    column you get code below but then run and no grid display

    <ItemTemplate><asp:Label runat="server" Text='<%# DataBinder.Eval(Container,
    "DataItem.reviseddate", "{0:d}") %>'></asp:Label></ItemTemplate>


    --
    cindy
    cindy, Apr 19, 2006
    #1
    1. Advertising

  2. Hi cindy,

    Thank you for posting.

    Regarding the data formatting in DataGrid column for DateTime data field, I
    think it is likely the data binding expression in the first code snippet
    you pasted hasn't been evaluated correctly by the runtime. I think you can
    try call the GetType method on the returned object and print it out to see
    whether it is of the correct type like DataBinder.Eval(Container.DataItem,
    "reviseddate").GetType().ToString(). Also, the casting error may also
    occur on other step in the databinding. You can define a helper function in
    the page's codebehind class to do the formatting. e.g:

    <%# MyConvertFunc( DataBinder.Eval(......)) %>

    in code behind, define:

    protected string MyConvertFunc(object obj)
    {
    .................
    }

    Please feel free to let me know if there is anything else I missed.

    Hope this helps.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.



    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Apr 20, 2006
    #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. Rob Roberts
    Replies:
    3
    Views:
    22,139
    Martin Arredondo
    Apr 13, 2007
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,179
    Michael Borgwardt
    May 30, 2004
  3. Eamonn
    Replies:
    5
    Views:
    31,422
    udayakumar86
    Nov 18, 2008
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,666
    Smokey Grindel
    Dec 2, 2006
  5. Short date format in template column?

    , May 18, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    604
    Bruno Alexandre
    May 19, 2006
Loading...

Share This Page