I understand this question has been asked before and I have referenced some answers that I have looked at that have not worked for me. However in a .Net 4.0 Web Forms application I have the same DataGrid, and the same code, but using .Net 4.5.1 WebForms application the formatting is different
.Net 4.0
<asp:BoundColumn DataField="Investment Date" DataFormatString="{0:d}" HeaderText="Investment Date" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>
produces a date format of dd/MM/yyyy - which is the result that I am after
.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>
and
<asp:BoundColumn DataField="Investment Date" DataFormatString="{0:d}" HeaderText="Investment Date" HeaderStyle-CssClass="centre" ItemStyle-CssClass="centre"></asp:BoundColumn>
produces a result of dd/MM/yyyy hh:mm:ss
I have looked at other stack questions here
but they have everything that I have tried already.
Just so that I could made sure I wasn't going crazy I have also checked the formatting here too
https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.boundfield.dataformatstring.aspx and this tells me that I am correct too.
This is how the data is added to the data table
protected void bttnAddExisting_Click(object sender, EventArgs e)
{
var ei = new ExistingInvestments();
dgExistingInvestments.DataSource = ei.dtExistingInvestments(Session, ddlExistingFundName, ddlExistingFundRiskProfile, ddlExistingFundCustomerName, txtExistingFundInvestmentDate);
dgExistingInvestments.DataBind();
}
and this is the class 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;
}
}
}
Any and all help greatly appreciated If anyone could help that would be great.
You did not specify the data type of the columns in the DataTable. Therefore formatting will be difficult. Make the column a DateTime column.
dtExistingFund.Columns.Add("Investment Date", typeof(DateTime));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With