如何将日期时间格式化为仅数据表中的特定DATE格式 [英] How to format datetime to only specific DATE format in datatable
问题描述
在我的datagridview1中,DATE列显示格式为 MM / dd / yyyy hh:mm:ss
In my datagridview1, the DATE column show format "MM/dd/yyyy hh:mm:ss"
然后我使用此代码。
我使用一个函数来填充数据网格视图
I use a function to fill a datagridview like this
public void load_table()
{
DataTable ltdgv = getLoad_table();
//the getLoad_table() function return a datatable type and store it to ltdgv
foreach (DataRow dr in ltdgv.Rows)
{
dr["DATE"] = DateTime.Parse((dr["DATE"].ToString())).ToShortDateString();
}
dataGridView1.DataSource = ltdgv;
}
,但DATE列仍显示格式MM / dd / yyyy [例如:11 / 27/2016]
but the DATE column still showing format MM/dd/yyyy [ex: 11/27/2016]
但我想将其更改为dd / MM / yyyy或27/11/2016
but I want to change it to dd/MM/yyyy or 27/11/2016
我试图将其更改为=
dr["DATE"] = DateTime.Parse((dr["DATE"].ToString("dd/MM/yyyy"))).ToShortDateString();
// I fill a parameter to the .ToString()
但是我现在得到一个语法错误。
But I now get a "syntax error".
那么,如何解决这个问题?
So, how to fix this?
推荐答案
请勿尝试更改数据表中datetime列的格式。
只是DateTime本身没有格式。
Do not try to change the 'format' of a datetime column in a datatable.
Simply a DateTime has no format by itself.
它是您用来显示日期的工具,该日期具有以可读格式显示任务的任务
It is the tool that you use to display the date that has the task to present it in a human readable format, whatever this format is.
换句话说。
您只需要设置
dataGridView1.Columns[0].DefaultCellStyle.Format = "dd/MM/yyyy";
(假设索引零的网格列是您的日期列)
(Assuming that the grid column at index zero is your date column)
这篇关于如何将日期时间格式化为仅数据表中的特定DATE格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!