【问题标题】:Showing date in MM/DD/YY format in a gridview column from .cs page/rowdatabound?在.cs page/rowdatabound的gridview列中以MM/DD/YY格式显示日期?
【发布时间】:2013-10-03 10:24:31
【问题描述】:

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

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

我正在我的.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 但仍然没有工作

【问题讨论】:

  • 您显示了两个不同的列,您要更改的一个似乎是名称为Date 的字符串列,网格中显示的另一个DateTime 列是MODIFIED。这很令人困惑,请澄清。
  • 对不起,我的错误..我正在修改它。它只是杀了我..

标签: c# asp.net gridview grid dataset


【解决方案1】:

方法1 :在aspx页面中。

在这种情况下最好的事情,我更喜欢做的是:

<asp:BoundField DataField="Modified" DataFormatString="{0:d}" 
   HeaderText="Modified On" HeaderStyle-BackColor="#D9EDF7"></asp:BoundField>

并在gridview中设置AutoGenerateColumns="false"

为所有列使用BoundField

方法2 :在codebehind中为电网带有行数据流,并执行必要 格式化如下:

date1.ToString("d", DateTimeFormatInfo.InvariantInfo);

干杯!!

【讨论】:

  • 我没有界面的字段dude :( span>
  • 网格必须从代码后面从绑定数据中获取字段。将自动生成列设置为false允许u使用boundfield。否则您可以始终使用行 - Databound事件。 “我正在使用时间戳从数据库获取日期时间字段。我的网格有一个自动生成的列。” span>
  • 可以在rowdatabound span>中提供一个例子
  • e.Row.Cells[e.Row.Cells.Count - 2].Text = DateTime.Parse(e.Row.Cells[e.Row.Cells.Count - 2].Text。 ToString()).ToString("MM/dd/yy")
  • 以上对我有用...但感谢您的帮助真的很有帮助...干杯。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-29
  • 2018-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多