2

I'm trying to add a DateTime column to a DataTable. I use that code:

DataTable dt = new DataTable();
dt.Columns.Add("date", typeof(DateTime));

But this requires a DateTime with the format "dd.MM.yyyy HH:mm:ss".

What I want is only the date. So typeof(DateTime) but only the date.

If you have a better way to do it, please tell me. My way is just an idea I had :)

Suggestions appreciated :)

EDIT:

I want to use the DataTable as DataSource of a Telerik RadGridView. I'm using the CellFormatting event and in it I try to fill the date in the RadGridView cells. Here you can see the snippet I use:

DateTime.ParseExact(e.CellElement.Value.ToString(), "dd.MM.yyyy HH:mm:ss", CultureInfo.CurrentCulture)

This will logically show the date combined with the time. But if I change it to

DateTime.ParseExact(e.CellElement.Value.ToString(), "dd.MM.yyyy", CultureInfo.CurrentCulture

it throws and exception that this is not a valid DateTime format because I used typeof(DateTime) above. So it requires the time as well or else it will throw an exception.

EDIT2:

It's a Windows Forms application.

Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
Therk
  • 391
  • 6
  • 23
  • 2
    `DateTime` doesn't have _any_ implicit format. String representations have. – Soner Gönül Oct 17 '14 at 07:56
  • 1
    A `DateTime` doesn't *have* a format. It's just a date and a time. If you want to only format the date part, then change the format in how you're viewing the datatable. – Jon Skeet Oct 17 '14 at 07:56
  • If I change the format in how I'm viewing the table it throws an exception that it's not a valid DateTime format. – Therk Oct 17 '14 at 08:00
  • If you are just storing it in a DataTable then you shouldn't worry about the format. Only when you need to display it somewhere does it matter – musefan Oct 17 '14 at 08:07

3 Answers3

2

A DateTime has no format, it just has a value. You can display a DateTime with a specific format, for example without the time. Or you can truncate the time portion of a DateTime by using its Date property. But note that this will not remove the time portition from it; it'll just return the DateTime which is midnight of the same date.

So you should still use DateTime as column-type and apply the format where you want to diplay this field.

For example:

foreach(DataRow row in dt.Rows)
{
    DateTime date = row.Field<DateTime>("date");
    string dateFormatted = date.ToShortDateString(); // or ToString("d") or dt.ToString("dd.MM.yyyy")
}
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • Thank you for you answer. I edited my question, please see above. I think I didn't write important things before so I edited it now :) – Therk Oct 17 '14 at 08:14
  • @Therk: one way is `DataFormatString="{0:d}"` or `DataFormatString="{0:dd.MM.yyyy}"` on the aspx at this column. It is ASP.NET, isn't it? – Tim Schmelter Oct 17 '14 at 08:17
  • I don't know what you excactly mean. Do you mean on the line `dt.Columns.Add("date", typeof(DateTime));`? Or what/where do you mean? – Therk Oct 17 '14 at 08:22
  • @Therk: is it ASP.NET? ... oh wait, `CellFormatting` seems to be a winforms event. Give me some time. – Tim Schmelter Oct 17 '14 at 08:23
  • It is .NET Windows Forms. There aren't any .aspx files. I should've mentioned it in the question. I'll edit again. – Therk Oct 17 '14 at 08:26
  • I'm not familiar with telerik controls. But [i've read here](http://www.telerik.com/forums/formatting-a-datetime-cell-as-date) that you can use: `GridViewDateTimeColumn column = (GridViewDateTimeColumn)this.radGridView1.Columns["Date"]; column.DataTextFormatString = "{0:dd.mm.yy}";` if the column type is `System.DateTime`. – Tim Schmelter Oct 17 '14 at 08:27
  • Thats what I found first. But that didn't help me. Maybe I should as the question in a Telerik forum. Thank you for your help :) – Therk Oct 17 '14 at 08:30
0

As mentioned in the comments, there's a difference between the data within a DateTime structure and the way that it is formatted. If you only care about the date, the standard is to leave the time at 00:00:00.000. In fact, there's a constructor for DateTime that does exactly this.

When you format the date, what you display is up to you. Using ToShortDateString might give you the output you want.

E.g.

DateTime myDate = new DateTime(2014, 12, 25);
string s = myDate.ToShortDateString(); // s = 12/25/2014
Scott Mermelstein
  • 15,174
  • 4
  • 48
  • 76
0

I would suggest keeping a valid DateTime object in the data source, and use the grid's abilities to change the format displayed in the cells. Here is a small example demonstrating how to achieve this.

        DataTable dt = new DataTable();
        dt.Columns.Add("date", typeof(DateTime));
        dt.Rows.Add(DateTime.Now);
        dt.Rows.Add(DateTime.Now.AddDays(1));

        RadGridView grid = new RadGridView();
        this.Controls.Add(grid);
        grid.DataSource = dt;

        GridViewDateTimeColumn dateColumn = (GridViewDateTimeColumn )grid.Columns["date"];

        //this will set the format of the date displayed in the cells
        dateColumn.FormatString = "{0:dd.MM.yyyy}";

        //this will set the format of the editor - when the cell is opened for editing
        dateColumn.Format = DateTimePickerFormat.Custom;
        dateColumn.CustomFormat = "dd.MM.yyyy";

More information about this column type is available in the Telerik UI for WinForms documentation.

checho
  • 3,092
  • 3
  • 18
  • 30