This is a migrated thread and some comments may be shown as answers.

Format Date Column

1 Answer 420 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Frank
Top achievements
Rank 1
Frank asked on 10 Jun 2015, 02:36 PM

I am trying to format a date column to MM/dd/yyyy but reguardless of what formatting I attempt it continues to return M/d/yyyy hh:mm:ss AM.

On load of the page I set the ObjetDataSource and convert to a datatable

public static DataTable LoadGrid()
        {
            using (Model.Entities db = new Model.Entities())
            {
                List<Model.FileDetailViewNew> fileView = (from filing in db.FileDetailViewNews
                                                          select filing).ToList();
 
                DataTable table = ToDataTable(fileView);
                return table;
            }
 
        }
 
        private static DataTable ToDataTable<T>(List<T> items)
        {
            DataTable dataTable = new DataTable(typeof(T).Name);
 
            //Get all the properties
            PropertyInfo[] Props = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo prop in Props)
            {
                //Setting column names as Property names
                dataTable.Columns.Add(prop.Name);
            }
            foreach (T item in items)
            {
                var values = new object[Props.Length];
                for (int i = 0; i < Props.Length; i++)
                {
                    //inserting property values to datatable rows
                    values[i] = Props[i].GetValue(item, null);
                }
                dataTable.Rows.Add(values);
            }
            //put a breakpoint here and check datatable
            return dataTable;
        }
 

 I then on the page set the grid to the datasource

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" TypeName="AllFilings" SelectMethod="LoadGrid"></asp:ObjectDataSource>
 
                                <div id="load" style="display: none; color: Red">
                                    <b> Updating filing status please wait...</b>
                                </div>
                                <div id="status" style="color: Red">
                                </div>
                                <telerik:RadGrid ID="gridFilings" runat="server" PageSize="20" AllowPaging="True" AllowSorting="True"
                                    AutoGenerateColumns="False" EnableAJAX="True" DataSourceID="ObjectDataSource1"
                                    EnableAJAXLoadingTemplate="True" GridLines="None" LoadingTemplateTransparency="50" EnableLinqExpressions="false"
                                    ShowStatusBar="True" Skin="Outlook" Width="98%" ShowGroupPanel="True">
                                    <ClientSettings AllowDragToGroup="True">
                                        <Selecting AllowRowSelect="True" />
                                    </ClientSettings>
                                    <PagerStyle Mode="NextPrevAndNumeric"></PagerStyle>
                                    <MasterTableView DataKeyNames="FileID" DataSourceID="ObjectDataSource1" AllowCustomSorting="true">
 

 and then on the grid item

 

<telerik:GridTemplateColumn DataField="DateIn" HeaderText="Date In" SortExpression="DateIn"                                                UniqueName="DateIn" GroupByExpression="DateIn Group By DateIn">
         <ItemTemplate>
                 <%# Eval("DateIn","{0:yyyy MMMM/dd}")%>
         </ItemTemplate>
         <HeaderStyle CssClass="headerLeft" />
</telerik:GridTemplateColumn>

 I have tried  numerous different formatting styles with no success.  I have also tried to change it to a DateTimeColumn and format it there and it stiil continues to show  for example 6/4/2014 12:00:00 AM

 

 

 

1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 15 Jun 2015, 07:20 AM
Hi Frank,

Could you please verify that the value from your datasource is a DateTime object? Keep in mind that data format string work only if the formated value is of type DateTime and it is not string. For your convenience I prepared a small runnable sample which demonstrates two columns. The values of the first column is DateTime and the values of the second column is string. As you can see only the first column values are format and the second one is not.

Regards,
Kostadin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Frank
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Share this question
or