DataGrid with date column that is sometimes blank

Discussion in 'ASP .Net Datagrid Control' started by Stephen Walch, Jan 19, 2005.

  1. A column in my data source is of type DateTime and "no date" is represented
    as DateTime(0). How can I get the DataGrid to display the column as blank
    (empty string) in the cases where DateTime.Ticks==0 ?

    Thanks!

    -Steve
    Stephen Walch, Jan 19, 2005
    #1
    1. Advertising

  2. Hi Steve,

    Thanks for your posting. As for the displaying DateTime value in DataGRid
    column according to its Ticks value question you mentioned, I think we
    should use a Template Columns instead of the simple BoundColumn. Are you
    currently using BoundColunn to display data? The BoundColumn only simpley
    display the certain field retrieved from the datasource via field name and
    we can do some additional simple formatting. If you have some more complex
    code logical to display the data value, I think it's quite easy to use the
    Template Column in which we can use databind statement to embeded our
    custom defined helper functions. For exmple:

    the following datagrid use a Template Column to display a dateTime field
    and use a helper function in the codebehind page class to format the output
    according to the dateTime value's Ticks:

    <asp:DataGrid id="DataGrid1" runat="server">
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <%# GetFormattedTime((DateTime)DataBinder.Eval(Container.DataItem,
    "date")) %>
    </ItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>

    protected string GetFormattedTime(DateTime date)
    {
    if(date.Ticks == 0)
    {
    return "Empty Time";
    }

    return date.ToShortTimeString();
    }


    And here is a certain tech article introduce the Templates in asp.net :

    #Understanding Templates in ASP.NET
    http://msdn.microsoft.com/msdnmag/issues/02/01/cutting/default.aspx


    Hope also helps. Thanks.

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    Steven Cheng[MSFT], Jan 20, 2005
    #2
    1. Advertising

  3. Stephen Walch

    Elton Wang Guest

    Hi Steve,

    One way to change DataGrid's display text (or format) can
    be done in dataGrid_ItemDataBound Event.

    As following code

    ListItemType itemType = e.Item.ItemType;
    if (itemType == ListItemType.Header || itemType ==
    ListItemType.Footer || itemType == ListItemType.Separator)
    return;

    DataRowView drv = (DataRowView)e.Item.DataItem;
    TableCell dateCell = (TableCell)e.Item.Controls;

    DateTime dateField = (DateTime)drv;

    if(dateField.Ticks == 0){
    dateCell.Text = "";
    }

    Hope it's helpful to you.

    Elton Wang



    >-----Original Message-----
    >A column in my data source is of type DateTime and "no

    date" is represented
    >as DateTime(0). How can I get the DataGrid to display

    the column as blank
    >(empty string) in the cases where DateTime.Ticks==0 ?
    >
    >Thanks!
    >
    >-Steve
    >
    >
    >.
    >
    Elton Wang, Jan 20, 2005
    #3
    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 Saunders
    Replies:
    0
    Views:
    450
    John Saunders
    Aug 28, 2003
  2. Nehmo Sergheyev
    Replies:
    1
    Views:
    491
    Andrew Urquhart
    May 9, 2004
  3. Marcin Vorbrodt

    ::std sometimes needed, sometimes not

    Marcin Vorbrodt, Sep 16, 2003, in forum: C++
    Replies:
    24
    Views:
    745
    Jerry Coffin
    Sep 17, 2003
  4. Replies:
    1
    Views:
    491
    gkelly
    Nov 29, 2006
  5. Randy Smith
    Replies:
    2
    Views:
    431
    Randy Smith
    Apr 24, 2007
Loading...

Share This Page