我正在从数据库中获取带有时间戳的日期时间字段。
我的网格有一个自动生成的列。
我正在我的 .cs
页面中对其进行格式化
dsWorkItems.Tables[0].Rows[count]["Date"] =
!string.IsNullOrEmpty(dsWorkItems.Tables[0].Rows[count]["Date"].ToString()) ?
((DateTime)(dsWorkItems.Tables[0].Rows[count]["Date"])).ToShortDateString(): "";
在快速观看中我得到
((DateTime)(dsWorkItems.Tables[0].Rows[count]["Date"])).ToShortDateString() as 9/26/2013
但在网格中我得到 9/26/2013 12:00:00 AM
但我想在网格中显示 9/26/13
还有一件事是可以在单元格工具提示中显示数据库中的完整日期时间戳值。
我已经尝试过
dsWorkItems.Tables[0].Columns["Date"].DefaultCellStyle.Format = "MM/dd/yy";
但是缺少 DefaultCellStyle 程序集,我添加了 System.Windows.Form 但仍然不起作用
最佳答案
方法一:在aspx页面中。
在这种情况下,我更喜欢做的最好的事情是:
<asp:BoundField DataField="Modified" DataFormatString="{0:d}"
HeaderText="Modified On" HeaderStyle-BackColor="#D9EDF7"></asp:BoundField>
并在 GridView 中设置AutoGenerateColumns="false"
。
对所有列使用 BoundField
。
方法 2:在 Codebehind 中为网格设置行数据绑定(bind)事件并执行必要的操作 格式如下:
date1.ToString("d", DateTimeFormatInfo.InvariantInfo);
干杯!!
关于c# - 在 .cs page/rowdatabound 的 gridview 列中以 MM/DD/YY 格式显示日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19045385/