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

标签 c# asp.net xml datetime dataset

我正在尝试使用 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/

相关文章:

xml - 用于模型交换的 Content MathML 与 OpenMath

xml - 如何抑制过于冗长的 xpath 输出?

c# - 为什么 `List<T>` 不可克隆?

c# - 安全地将信用卡号转移到 Paypal

c# - 在 DI 环境中使用时出现奇怪且难以调试的错误

c# - 同时读取 FromUri 和 FromBody

c# - 在 C# 中执行异步触发时省略 await 语句并忘记使用 ado.net 写入数据库是否安全

c# - 绑定(bind)数组+一个对象到Dropdownlist

c# - 从 c# 打开 .exe(c 程序)

xml - Groovy Node.depthFirst() 返回节点和字符串列表?