c# - ASP.Net Datagrid DataFormat String 未按要求格式化日期

标签 c# asp.net .net .net-4.5 dataformat

我知道这个问题以前有人问过,我引用了一些我看过但对我不起作用的答案。然而,在 .Net 4.0 Web Forms 应用程序中,我有相同的 DataGrid 和相同的代码,但使用 .Net 4.5.1 WebForms 应用程序时格式不同

.Net 4.0

<asp:BoundColumn DataField="Investment Date" DataFormatString="{0:d}" HeaderText="Investment Date" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>

产生 dd/MM/yyyy 的日期格式——这就是我想要的结果

.Net 4.5.1

<asp:BoundColumn DataField="Investment Date" DataFormatString="{0:dd-MM-yyyy}" HeaderText="Investment Date" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>

<asp:BoundColumn DataField="Investment Date" DataFormatString="{0:d}" HeaderText="Investment Date" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>

产生 dd/MM/yyyy hh:mm:ss 的结果

我在这里查看了其他堆栈问题

但他们拥有我已经尝试过的一切。

为了确保我不会发疯,我也检查了这里的格式

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx这告诉我我也是正确的。

数据是这样添加到数据表中的

 protected void bttnAddExisting_Click(object sender, EventArgs e)
    {
        var ei = new ExistingInvestments();
        dgExistingInvestments.DataSource = ei.dtExistingInvestments(Session, ddlExistingFundName, ddlExistingFundRiskProfile, ddlExistingFundCustomerName, txtExistingFundInvestmentDate);
        dgExistingInvestments.DataBind();
    }

这是 ExistingInvestments 类

public class ExistingInvestments
{

    public string FundName { get; set; }
    public string FundRiskProfile { get; set; }
    public string CustomerName { get; set; }
    public DateTime InvestmentDate { get; set; }

    public DataTable dtExistingInvestments(HttpSessionState Session, DropDownList ddlExistingFundName, DropDownList ddlExistingFundRiskProfile,
            DropDownList ddlExistingFundCustomerName, TextBox txtExistingFundInvestmentDate)
    {
        if (Session["ExistingInvestmentsTable"] == null)
        {
            var dtExistingFund = new DataTable();

            dtExistingFund.Columns.Add("Fund Name");
            dtExistingFund.Columns.Add("Fund Risk Profile");
            dtExistingFund.Columns.Add("Customer Name");
            dtExistingFund.Columns.Add("Investment Date");
            if (string.IsNullOrEmpty(txtExistingFundInvestmentDate.Text))
            {
                dtExistingFund.Rows.Add(ddlExistingFundName.SelectedValue, ddlExistingFundRiskProfile.SelectedValue,
                    ddlExistingFundCustomerName.SelectedValue, string.Empty);
            }
            else
            {
                dtExistingFund.Rows.Add(ddlExistingFundName.SelectedValue, ddlExistingFundRiskProfile.SelectedValue,
                    ddlExistingFundCustomerName.SelectedValue, Convert.ToDateTime(txtExistingFundInvestmentDate.Text));
            }

            Session["ExistingInvestmentsTable"] = dtExistingFund;
            return dtExistingFund;
        }
        else
        {
            var dt = (DataTable)Session["ExistingInvestmentsTable"];

            if (string.IsNullOrEmpty(txtExistingFundInvestmentDate.Text))
            {
                dt.Rows.Add(ddlExistingFundName.SelectedValue, ddlExistingFundRiskProfile.SelectedValue,
                    ddlExistingFundCustomerName.SelectedValue, string.Empty);
            }
            else
            {
                dt.Rows.Add(ddlExistingFundName.SelectedValue, ddlExistingFundRiskProfile.SelectedValue,
                    ddlExistingFundCustomerName.SelectedValue, Convert.ToDateTime(txtExistingFundInvestmentDate.Text.Trim()));
            }

            Session["ExistingInvestmentsTable"] = dt;
            return dt;
        }          
    }
}

非常感谢任何帮助 如果有人可以提供帮助,那就太好了。

最佳答案

您没有指定 DataTable 中列的数据类型。因此格式化会很困难。将该列设为 DateTime 列。

dtExistingFund.Columns.Add("Investment Date", typeof(DateTime));

关于c# - ASP.Net Datagrid DataFormat String 未按要求格式化日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741044/

相关文章:

javascript - 在 IIS 中启用 JavaScript GZIP 压缩?

c# - 如何从 WCF 服务流下载文件?

.net - .NET 中通过 WMI 的数据库大小

c# - 检查子元素之一是否为 null 且不抛出异常的最佳方法

c# - 如何以当前区域性格式的默认值从 Noda Time OffsetDateTime 生成 DateTimeOffset 字符串?

asp.net - Entity Framework : how to solve "FOREIGN KEY constraint may cause cycles or multiple cascade paths"?

asp.net - 无法加载文件或程序集System.Runtime,版本=4.1.2.0

.net - .NET 框架是否具有低级多媒体和鼠标支持?

c# - 在 Visual Studio 2013 中生成 .edmx EF6 的问题

java - 即使网络连接断开,TCP 套接字在发送消息时也不会引发任何异常