C# 如何以特定格式在日期时间列中绑定网格
我正在尝试使用XML作为数据源在C#中绑定网格C# 如何以特定格式在日期时间列中绑定网格,c#,asp.net,xml,datetime,dataset,C#,Asp.net,Xml,Datetime,Dataset,我正在尝试使用XML作为数据源在C#中绑定网格 string sApplication = txtApplicationsta.Text.Trim(); if (sApplication=="") { DataSet ds3 = new DataSet(); ds3.ReadXml(Server.MapPath("Status.xml")); if (ds3.Tables[0].Rows.Count != 0) { gvstatus.
string sApplication = txtApplicationsta.Text.Trim();
if (sApplication=="")
{
DataSet ds3 = new DataSet();
ds3.ReadXml(Server.MapPath("Status.xml"));
if (ds3.Tables[0].Rows.Count != 0)
{
gvstatus.DataSource = ds3;
gvstatus.DataBind();
}
}
数据绑定成功。我正在将日期格式保存为YYYY-MM-DD
如下图所示:
string idate = DateTime.Parse(ssplit[4].Trim()).ToString("yyyy-mm-dd", CultureInfo.InvariantCulture);
现在,我需要以DD-MM-YYYY
格式绑定我的网格列,但在网格中,它将根据我保存的内容进行绑定
我正在Asp页面中绑定网格,如下所示:
<asp:TemplateField HeaderText='Last Update' HeaderStyle-VerticalAlign="Middle">
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" CssClass="chkbox" />
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" att='<%#DataBinder.Eval(Container.DataItem,"ID")%>' Text='<%# SetLinkCodestatus(Convert.ToInt64(DataBinder.Eval(Container.DataItem,"ID")),DataBinder.Eval(Container.DataItem,"LastUpdate").ToString()) %>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="3%" HorizontalAlign="left" />
</asp:TemplateField>
如果我将值保存为
YYYY-MM-DD
格式,如何使用DD-MM-YYYY
格式绑定网格?对于在GridView
列中简单显示日期值,您已经做了太多的工作。不要将模板字段
与标签
控件一起使用,而是使用简单的边界字段
然后,您可以使用BoundField
列中的dataformatstring
属性设置列的日期格式:
<asp:GridView ...>
<Columns>
<asp:BoundField DataField="My_Date_Field" dataformatstring="{0:dd-MM-yyyy}" />
▲
▲
我们可以借助DataFormatString实现格式化
<asp:BoundField DataField="Amount" HeaderText="Amount" DataFormatString="{0:###,###,##0.000}">
Convert.ToDateTime(DataBinder.Eval(Container.DataItem,“LastUpdate”)).ToString(“dd-MM-yyyy”))