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;
}