Jos*_*son 3 c# asp.net datetime
我有一个包含datetime字段的表.我已将其放入datakey和pageload并从gridview中选择它将使用数据库中表的日期时间填充文本框.我不想要时间.在网格视图中,我已经摆脱了时间,但是当我填充文本框时,我无法摆脱它.有什么建议?这是我的代码:(我需要PurchaseDate和WarrantyDate只显示没有时间的日期)
private void PopulateForm(int index)
{
var dataKey = grdvwAssetGrid.DataKeys[index];
if (dataKey != null)
AssetId = (int)dataKey["Id"];
if (dataKey != null)
LocationId = (int)dataKey["LocationId"];
if (dataKey != null)
txtVendor.Text = (string)dataKey["Vendor"];
if (dataKey != null)
txtSerialNumber.Text = (string)dataKey["SerialNumber"];
if (dataKey != null)
txtDescription.Text = (string)dataKey["Description"];
if (dataKey != null)
txtType.Text = (string)dataKey["Type"];
if (dataKey != null)
txtPrimaryUser.Text = (string)dataKey["PrimaryUser"];
if (dataKey != null)
txtPurchaseDate.Text = dataKey["PurchaseDate"].ToString();
if (dataKey != null)
txtWarrantyDate.Text = dataKey["WarrantyExpDate"].ToString();
}
Run Code Online (Sandbox Code Playgroud)
在gridview中,我这样做是为了将日期时间更改为仅日期:
<asp:BoundField DataField="PurchaseDate" HeaderText="Purchase Date" InsertVisible="False" ReadOnly="True" DataFormatString="{0:d}" />
<asp:BoundField DataField="WarrantyExpDate" HeaderText="Warranty Date" InsertVisible="False" ReadOnly="True" DataFormatString="{0:d}" />
Run Code Online (Sandbox Code Playgroud)
ToShortDateString()而不是ToString().
要么
ToString("d"),这是短日期字符串的格式.
更新:
将您的值转换为DateTime对象,如下所示:
if (dataKey != null)
txtPurchaseDate.Text = ((DateTime)dataKey["PurchaseDate"]).ToShortDateString();
if (dataKey != null)
txtWarrantyDate.Text = ((DateTime)dataKey["WarrantyExpDate"]).ToString("d");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8215 次 |
| 最近记录: |