Datagrid date column format

Posted on 2004-11-17
Last Modified: 2012-05-05

I am using VB and ASP.Net and have a stored procedure that returns either a null or a date.  I have tried formatting the date as {0:yyyy-MM-dd h:mm tt} but when there is no date returned (as happens in some rows), it defaults to 1900-01-01....

How do I put conditional logic on this?  I am currently populating the grid as follows:
            DataGrid1.DataSource = sqlcmd.ExecuteReader

Appreciate the help.

Question by:porter416
    LVL 28

    Assisted Solution

    Hi porter416,

    i suggest you to work in this case with ItemDataBound event
    there you can determine if you will output a date or not depending on the value for the current record

    here is an example ( it is based on datalist, BUT it is absolutely the same in your case with datagrid - just change
    DataGridItemEventArgs )

    LVL 26

    Accepted Solution

    try this ... u could use the isnull for the date field in the SQL statement that is used to retrieve the values from the database ..
    select isnull(mydatefield, ' ' ) as mydatefield from mytable
    this will ensure that an emplty value is returned instead of a null .. just check how the formatting behaves in this case ..


    Author Comment

    Both were great answers and the second 1 was simpler and ultimately the 1 I used.

    Thank you both.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
    A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now