0

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.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862

4 Answers4

3

The Date property is still of type DateTime which always has the time properties and therefore your UI will pick it up I guess.

To display only the date, use .ToString("d")

MichaC
  • 13,104
  • 2
  • 44
  • 56
  • The member being assigned to is a DateTime; changing the datatypes in the class from DateTime (which corresponds to their true identity, and what they are in the SQL Server table) seems rather drastic for what should be a pretty quick fix. – B. Clay Shannon-B. Crow Raven Feb 22 '16 at 17:11
  • well usually you separate database and UI. Don't know what this is you are using for reporting but there should usually be a simple way to abstract that and define the output. Maybe it is just configuration somewhere where you can say which date format should be used? You question was about the DateTime type, and that's the answer to that ;) – MichaC Feb 22 '16 at 17:19
  • is that the answer you are looking for? http://stackoverflow.com/questions/4033113/how-to-format-datetime-columns-in-datagridview – MichaC Feb 22 '16 at 17:26
2

in your class, use the DisplayFormat data annotation.

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString="{0:dd/MMM/yyyy}")]
public DateTime NextExecutionCalcd { get; set; }

https://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.displayformatattribute.dataformatstring(v=vs.110).aspx

user1666620
  • 4,800
  • 18
  • 27
1

I had the same problem. I don't know if it's the best solution, but I cut off the time-property with regex like this:

Regex.Match(Convert.ToDateTime("01.01.2014 00:00:00").Date.ToString(), @"\d{2}\.\d{2}\.\d{4}").ToString();

This line of code returns only the date (in this case 01.01.2014) as a string.

Regex.Match() compares a string with a pattern and returns a match: in this case it searches for two digits(\d{2}), a point(.), another two digits(\d{2} another point (.) and 4 digits (\d{4}).

To use Regex you need the using-directive using System.Text.RegularExpressions;

Tamwe
  • 359
  • 2
  • 4
1

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;
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862