Search code examples
c#sql-serverdatetimedatagridviewdatagridviewcolumn

How can I display the Date portion only of DateTime values?


I've got this class:

public class ScheduledReports
{
    public string Unit { get; set; }
    public string ReportName { get; set; }
    public DateTime NextExecutionCalcd { get; set; }
    public string AllEmailAddresses { get; set; }
    public DateTime DataRangeBeginDateCalcd { get; set; }
    public DateTime DataRangeEndDateCalcd { get; set; }
}

The underlying affiliated SQL Server table uses DateTime fields.

I populate instances of the class like so:

while (sqlReader.Read())
{
    ScheduledReports sr = new ScheduledReports();
    . . . // Other class member assignments elided for brevity
    sr.DataRangeBeginDateCalcd = GetNextDataRangeBeginDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsBeginDateArg"]).Date); 
    sr.DataRangeEndDateCalcd = GetNextDataRangeEndDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsEndDateArg"]).Date); 
    scheduledRpts.Add(sr);
}

As you can see, I'm using "Convert.ToDateTime(bla).Date" instead of simply "Convert.ToDateTime(bla)" in hopes of thus truncating the time portion away, but it doesnt' work; I assign what's returned from the method whose snippet is shown above thus:

private void LoadScheduledTab()
{
    List<ScheduledReports> scheduledRpts = RoboReporterSQL.GetScheduledReports();
    dataGridViewScheduled.DataSource = scheduledRpts;
}

...yet the grid shows the DateTime values wiht the time values, which are useless to me; I just want the Date and nothing but the date.

How can I convince the the display to cease from divulging TMI?

UPDATE

I tried to implement user 1666620's answer like so (after adding System.ComponentModel.DataAnnotations) as a reference and resolving "DisplayFormat"):

public class ScheduledReports
{
    public string Unit { get; set; }
    public string ReportName { get; set; }
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MMM/yyyy}")]
    public DateTime NextExecutionCalcd { get; set; }
    public string AllEmailAddresses { get; set; }
    public DateTime DataRangeBeginDateCalcd { get; set; }
    public DateTime DataRangeEndDateCalcd { get; set; }
}

...but it still fails; it compiles, but still displays the values in the DataGridView with both the date and the time.


Solution

  • I needed to move the call to ".Date" over past the last paren, so instead of this:

    sr.DataRangeBeginDateCalcd = GetNextDataRangeBeginDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsBeginDateArg"]).Date);
    sr.DataRangeEndDateCalcd = GetNextDataRangeEndDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsEndDateArg"]).Date);
    

    ...it's this:

    sr.DataRangeBeginDateCalcd = GetNextDataRangeBeginDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsBeginDateArg"])).Date;
    sr.DataRangeEndDateCalcd = GetNextDataRangeEndDateAfterQueued(unit, rptName, Convert.ToDateTime(sqlReader["NextExecutionsEndDateArg"])).Date;