我正在尝试使用 XML 作为数据源在 C# 中绑定(bind)网格
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();
}
}
数据绑定(bind)成功。我将日期格式保存为 YYYY-MM-DD
。
如下:
string idate = DateTime.Parse(ssplit[4].Trim()).ToString("yyyy-mm-dd", CultureInfo.InvariantCulture);
现在我需要以 DD-MM-YYYY
格式绑定(bind)我的网格列,但在网格中它通过我保存的内容进行绑定(bind)。
我在 Asp 页面中绑定(bind)网格,如下所示:
<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
格式绑定(bind)网格?
最佳答案
为了在 GridView
列中简单地显示日期值,您已经做了太多工作。使用简单的 BoundField
,而不是使用带有 Label
控件的 TemplateField
。
然后,您可以使用 BoundField
列中的 dataformatstring
属性设置列的日期格式:
<asp:GridView ...>
<Columns>
<asp:BoundField DataField="My_Date_Field" dataformatstring="{0:dd-MM-yyyy}" />
▲
关于c# - 如何以特定格式绑定(bind)日期时间列中的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45429080/