【问题标题】:How to bind a grid in column in date time in particular format如何以特定格式绑定日期时间列中的网格
【发布时间】:2018-01-07 19:18:36
【问题描述】:

我正在尝试使用 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.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 格式绑定网格?

【问题讨论】:

    标签: c# asp.net xml datetime dataset


    【解决方案1】:

    我们可以借助DataFormatString实现格式化

    <asp:BoundField DataField="Amount" HeaderText="Amount" DataFormatString="{0:###,###,##0.000}">
    

    【讨论】:

    • Convert.ToDateTime(DataBinder.Eval(Container.DataItem,"LastUpdate")).ToString("dd-MM-yyyy") )
    【解决方案2】:

    为了在GridView 列中显示日期值,您已经做了太多工作。不要使用带有Label 控件的TemplateField,而是使用简单的BoundField

    然后,您可以使用 BoundField 列中的 dataformatstring 属性设置列的日期格式:

    <asp:GridView ...>
        <Columns>
            <asp:BoundField DataField="My_Date_Field" dataformatstring="{0:dd-MM-yyyy}" />
                                                             ▲
    

    【讨论】:

      猜你喜欢
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多