【问题标题】:ASP.Net Datagrid DataFormat String not formatting date as requiredASP.Net Datagrid DataFormat String 未按要求格式化日期
【发布时间】:2017-01-19 21:19:16
【问题描述】:

我知道之前有人问过这个问题,并且我参考了一些我看过但对我不起作用的答案。但是在 .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;
        }          
    }
}

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

【问题讨论】:

  • 你忘了在问题中写你想要的实际结果,或者我有点灵巧而错过了它:)
  • 好点!我在 dd/MM/yyyy 之后立即编辑问题

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


【解决方案1】:

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

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

【讨论】:

  • 木头和树木...非常感谢。效果很好
猜你喜欢
  • 2021-03-22
  • 2022-06-22
  • 2013-02-20
  • 2010-10-25
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-12
  • 1970-01-01
相关资源
最近更新 更多