2

I have a date store in MySQL Database with the format Datetime.

I want to put only the date in one DataGridView but when I try i get the date with the following hours, something like:

29/06/2016 12:01:21

I tried to find the date directly from the MySQL query using the DATE() function, but it still does not work because in my DataGridView I see the date formatted as follows:

29/06/2016 00:00:00

Finally I tried side code to set the column, but without result

dataGridView.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy";

I use DataSource to "link" my data to my DataGridView.


This is the code:

var dataTable = listOfResult.ToDataTable(); //ToDataTable() Function that convert List into DataTable
dataGridView.DataSource = dataTable;
dataGridView.Columns[1].DefaultCellStyle.Format = "dd/MM/yyyy";

The listOfResult is a list of such dynamic , where inside there are all the dates.

Function ToDataTable()

public static DataTable ToDataTable(this IEnumerable<dynamic> items)
    {
        var data = items.ToArray();
        if (!data.Any()) return null;

        var dt = new DataTable();
        foreach (var key in ((IDictionary<string, object>) data[0]).Keys)
            dt.Columns.Add(key);

        foreach (var d in data)
            dt.Rows.Add(((IDictionary<string, object>) d).Values.ToArray());

        return dt;
    }
Lorenzo Belfanti
  • 1,205
  • 3
  • 25
  • 51
  • http://stackoverflow.com/questions/5050102/convert-datetime-to-date-format-dd-mm-yyyy – Offir Jun 29 '16 at 10:11
  • @OffirPe'er - I use DataBinding to link my "date" to my DataGridView. I read from my database the date and the put into datagridview. – Lorenzo Belfanti Jun 29 '16 at 10:13
  • 1
    Please show the code – Mairaj Ahmad Jun 29 '16 at 10:15
  • @Leopard - Added the code – Lorenzo Belfanti Jun 29 '16 at 10:18
  • What is `listOfResult`? – Aimnox Jun 29 '16 at 10:18
  • @Aimnox - A list of such dynamic , where inside there are all the dates – Lorenzo Belfanti Jun 29 '16 at 10:20
  • 2
    Have you checked that `ValueType = typeof(DateTime);` for the column ? Otherwise the format will fail silently. Also: Try to set those properties before loading the data. Also: where do you set the datatype of the DataTable column?? – TaW Jun 29 '16 at 10:42
  • @TaW - I use DataSource, so i don't create manualy the columns. I try to use datagrid.Columns[1].ValueType = typeof(DateTime); but it doesn't work – Lorenzo Belfanti Jun 29 '16 at 14:10
  • 1
    @LorenzoBelfanti I marked the question as duplicate of the linked post. The reason is exactly because the type of column is not `DateTime`. When you add a column using `Columns.Add("column name")`, the type of column will be `string` and so formatting will not apply. To solve the problem, you can use the method which is in linked post or simply fix your method to support type of properties. Let me know if you have any question about the answer :) – Reza Aghaei Jun 29 '16 at 23:58

1 Answers1

1

TaW's right. Set the DataType: replace

dt.Columns.Add(key);

with

dt.Columns.Add(key, typeof(DateTime));

for the desired columns.

Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
  • I use DataSource, so i don't create manualy the columns. I try to use `datagrid.Columns[1].ValueType = typeof(DateTime);` but it doesn't work – Lorenzo Belfanti Jun 29 '16 at 13:58
  • This is not about the DGV's columns but about the DataTable's column so I think this should work! – TaW Jun 29 '16 at 16:44
  • @TaW - I did as suggested, in the function that converts the list into `DataTable` I have conducted an audit that verifies if the data is of type `DateTime` in this case imposed the column, while creating as such – Lorenzo Belfanti Jun 30 '16 at 11:48