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

ASP.Net Datagrid DataFormat String not formatting date as required

我知道这个问题以前有人问过,我参考了一些我看过但对我不起作用的答案。然而,在 .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 这告诉我我也是正确的。

数据加起来是这样的table

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

这是 class 现有投资

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));