2017-01-19 2 views
3

Je comprends que cette question a déjà été posée et j'ai référencé des réponses que j'ai examinées qui n'ont pas fonctionné pour moi. Cependant, dans un .Net 4.0 application Web Forms J'ai le même DataGrid, et le même code, mais en utilisant .Net 4.5.1 applications WebForms la mise en forme est différenteASP.Net Datagrid DataFormat Chaîne ne formatant pas la date comme requis

.Net 4,0

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

produit une date format jj/mm/aaaa - ce qui est le résultat que je suis après

.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> 

et

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

produit un résultat de jj/mm/aaaa hh: mm: ss

J'ai regardé d'autres questions pile ici

mais ils ont tout que j'ai déjà essayé.

Juste pour que je puisse en assurer je ne deviens fou j'ai également vérifié la mise en forme ici aussi

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx et cela me dit que je ne me trompe pas trop.

Voici comment les données sont ajoutées à la table de données

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

et c'est la classe 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; 
     }   
    } 
} 

Toute aide grandement appréciée Si quelqu'un pouvait aider ce serait génial .

+0

Vous avez oublié d'écrire dans la question le résultat réel que vous voulez, ou je suis peut-être un peu adroite et MANQUÉ :) –

+0

bon point! Im après jj/MM/yyyy et ill modifier la question maintenant –

Répondre

2

Vous n'avez pas spécifié le type de données des colonnes dans le DataTable. Par conséquent, le formatage sera difficile. Faites de la colonne une colonne DateTime.

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

bois et arbres ... merci beaucoup. ça a fonctionné parfaitement –