Format Date Column

2 posts, 0 answers
  1. Frank
    Frank avatar
    101 posts
    Member since:
    Mar 2012

    Posted 10 Jun 2015 Link to this post

    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

     

     

     

  2. Kostadin
    Admin
    Kostadin avatar
    1733 posts

    Posted 15 Jun 2015 Link to this post

    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
Back to Top