从数据库中提取数据时如何仅在GridView中显示日期?C#

SS1*_*113 6 c# sql asp.net gridview date

我从访问数据库中提取数据,以显示在ASP.NET项目的GridView控件中.它工作正常,但我想看看我是否可以格式化正在拉动的数据.目前,任何货币都会从xx.xx截断为美元金额.日期显示mm/dd/yyyy hh/mm/ss AM/PM

我尝试将数据库本身编辑为正确的值(我将货币字段设置为"货币",将日期字段设置为"短日期",但是当我拉出该日期时,它仍然显示它们没有格式化.

编辑:对不起,不得不把代码删掉

有任何想法吗?谢谢

Ama*_*ian 9

在你的网格视图中添加名为的属性 DataFormatString

DataFormatString示例:


{0:dd MMMM yyyy}    -    gives 24 February 2006
{0:MMM dd}          -    gives Feb 24 (substitue MMM with MMMM for the full month name 
                         instead of abbreviation) 
{0:dd/MM/yy}        -    gives 24/02/06 
{0:dd/MM/yyyy}      -    gives 24/02/2006

示例代码

<asp:BoundField HeaderText="Date" 
                DataField="SampleDate" 
                DataFormatString="{0:MM/dd/yyyy}"  >
Run Code Online (Sandbox Code Playgroud)

MSDN BoundField.DataFormatString属性