c# - 在 .cs page/rowdatabound 的 gridview 列中以 MM/DD/YY 格式显示日期?

标签 c# asp.net gridview grid dataset

我正在从数据库中获取带有时间戳的日期时间字段。

我的网格有一个自动生成的列。

我正在我的 .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/

相关文章:

ASP.net 试图在页面上打印变量值

c# - 为什么这个下拉列表没有完成它应该做的事情?

android - 如何在 android 中的 gridview 项目上给予阴影效果

c# - GridView.Databind() 不适用于对象列表

c# - 重载的 RadTextBox 控件中的服务器端自定义验证器不调用其客户端验证函数

c# - 自定义检查器将值恢复为 Unity 中 Play 上之前的值

c# - 应用程序在虚拟应用程序 (IIS) 中运行时缺少程序集引用

c# - 解析 RSS 提要最近引发文档类型定义 (DTD) 错误

c# - 从 asp.net c# 页面调用客户端 javascript

c# - 如何阻止我的 ObjectDataSource 绑定(bind)两次?