C# 在.cs page/rowdatabound的gridview列中以MM/DD/YY格式显示日期?
我从数据库中得到一个带有时间戳的日期时间字段 我的网格有一个自动生成的列 我正在我的C# 在.cs page/rowdatabound的gridview列中以MM/DD/YY格式显示日期?,c#,asp.net,gridview,grid,dataset,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].R
.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
还有一件事是可以在单元格工具提示中显示来自数据库的完整datetimestamp值
我试过了
dsWorkItems.Tables[0].Columns["Date"].DefaultCellStyle.Format = "MM/dd/yy";
但是DefaultCellStyle程序集丢失,我添加了System.Windows.Form,但仍然无法工作。方法1:在aspx页面中 在这种情况下,我喜欢做的最好的事情是:
<asp:BoundField DataField="Modified" DataFormatString="{0:d}"
HeaderText="Modified On" HeaderStyle-BackColor="#D9EDF7"></asp:BoundField>
干杯 您显示两个不同的列,您正在更改的一个列似乎是名为
Date
的字符串列,网格中显示的另一个DateTime
列是MODIFIED
。这很混乱,请澄清。对不起,我的不好。我正在修改它。这简直要了我的命..我没有绑定字段dude:(网格必须从代码背后的绑定数据中获取字段。将“自动生成列”设置为false允许您使用boundfield。否则您可以始终使用row databound事件。“我从数据库中获取带有时间戳的日期-时间字段。我的网格有一个自动生成的列。”你能举个例子说明如何在rowdatabounde.Row.Cells[e.Row.Cells.Count-2].Text=DateTime.Parse(e.Row.Cells[e.Row.Cells.Count-2].Text.ToString()).ToString(“MM/dd/yy”)上面的方法帮了我的忙…但谢谢你的帮助,真的很有帮助…干杯。
date1.ToString("d", DateTimeFormatInfo.InvariantInfo);