gpt4 book ai didi

c# - 如何以特定格式在日期时间的列中绑定(bind)网格

转载 作者:行者123 更新时间:2023-11-30 21:39:01 25 4
gpt4 key购买 nike

我正在尝试使用 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 列中显示日期值做了太多工作。不要将 TemplateFieldLabel 控件一起使用,而是使用简单的 BoundField

然后,您可以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com