Pages

Saturday, November 23, 2013

asp DataGrid Format Text Field as DateTime

Here I'm binding to a DataGrid on an asp page, the Database contains a text field that holds a date text on the format "yyyyMMdd" and I want to show the date in the format "day MonthName year",

All I had to do is adding to the Datagrid <Columns> a TemplateColumn with the following binding:

<# DateTime.ParseExact(DataBinder.Eval(Container.DataItem, "DATE_Field").ToString(), "yyyyMMdd", new System.Globalization.CultureInfo("ar-sa")).ToString("dd MMM yyyy", new System.Globalization.CultureInfo("ar-sa"))%>

For example, let's say the database stores "14350117" in the field DATE_Field as text, the above binding will let it appear as  on the page..