DataFormatString Not working

16 posts, 1 answers
  1. Avidan
    Avidan avatar
    19 posts
    Member since:
    Sep 2012

    Posted 16 Nov 2009 Link to this post

     Hi,

    I bound a radGrid to ObjectDataSource control that returns list of User objects. One of User properties is PHONE which returns user's phone number or NULL as string.

    I bound the PHONE property to a grid bound column, as follow:

    <telerik:GridBoundColumn   
          DataField="PHONE"   
          UniqueName="PHONE"   
          HeaderText="Phone" 
          DataFormatString="{0:###-###-####}">  
    </telerik:GridBoundColumn> 

    however, it seem that the value is not formmatted...what I'm doing wrong?

    Avidan

     

     

     

     

     

     

     

  2. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 17 Nov 2009 Link to this post

    Hi Avidan,

    In order string format to be applied the data should be of the correct type. Thus in the your case the data should be of some numeric type (int, double, float etc) in order the supplied format string to work. Can you try setting the format from code behind as shown below and see whether it is working for you?

    C#:
     
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
        if (e.Item is GridDataItem) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            double dbl = Double.Parse(dataItem["PHONE"].Text.ToString()); 
            string str = String.Format("{0:###-###-##}", dbl); 
            dataItem["PHONE"].Text = str; 
        } 

    Regards,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Vyas
    Vyas  avatar
    6 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    Hi,

    My telerik version is "2010.2.713.40".

    I am using a telerik radgrid in which one column is GridDateTimeColumn. I want to format the date as Feb. 28, 2011. But the date displayed in my radGrid is   2/8/2011 12:00:00 AM. I tried the DataFormatString along with HtmlEncode set as False. I also used the DataType="System.DateTime" property.

    Please see the code below:

    <telerik:GridDateTimeColumn FilterControlWidth="120px" DataField="MyDate" HeaderText="MyDate"
       SortExpression="DateAdded" DataType="System.DateTime" UniqueName="MyDate"
      PickerType="DatePicker" DataFormatString="{0: dd,MMM,yyyy}" HtmlEncode="false">
        <HeaderStyle Width="160px" />
             </telerik:GridDateTimeColumn>
    I tried formatting the date through the backend using the following code: 

    protected void JobSummaryGrid_ItemDataBound(object sender, GridItemEventArgs e)
    {
        string name = e.
        if (name == "values")
        {
            GridColumnCollection cols = e.Item.OwnerTableView.Columns;
            foreach (GridColumn col in cols)
            {
                string type = col.ColumnType.ToString();
                if (type == "GridDateTimeColumn")
                {
                    GridDateTimeColumn coluna = (GridDateTimeColumn)col;
                    coluna.DataFormatString = "{0: MMM dd,yyyy}";
                }
            }
        }

    and

    protected void JobSummaryGrid_DetailTableDataBind(object source, GridDetailTableDataBindEventArgs e)
    {
        string name = e.DetailTableView.Name;
        if (name == "values")
        {
            GridColumnCollection cols = e.DetailTableView.Columns;
            foreach (GridColumn col in cols)
            {
                string type = col.ColumnType.ToString();
                if (type == "GridDateTimeColumn")
                {
                    GridDateTimeColumn coluna = (GridDateTimeColumn)col;
                    coluna.DataFormatString = "{0: MMM dd,yyyy}";
                }
            }
        }
    }


    But nothing worked. :-(

    I can only use GridDateTimeColumn as I need a date picker in the filter column of the grid. So please
    donnot suggest me to use anything other than that.

    Please give me some solution to solve this.

    I wanted my date to be displayed as Feb. 8, 2011 not 02/08/2011 12:00:00 AM

    Regards,
    Vyas.

  5. Vyas
    Vyas  avatar
    6 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    I even tried the code you mentioned as follows:

    protected void JobSummaryGrid_ItemDataBound(object sender, GridItemEventArgs e)
       {
           if (e.Item is GridDataItem)
           {
               GridDataItem dataItem = (GridDataItem)e.Item;
               double dbl = Double.Parse(dataItem["DateAdded"].Text.ToString());
               string str = String.Format("{0:MMM dd,yyyy}", dbl);
               dataItem["DateAdded"].Text = str;
           }
       }

    but nothing worked.

    Regards,
    Vyas
  6. Nikolay Rusev
    Admin
    Nikolay Rusev avatar
    2285 posts

    Posted 10 Feb 2011 Link to this post

    Hi Vyas ,

    Seems DataFormatString to be working properly on the online demos. See GridDateTimeColumn on the second grid:
    http://demos.telerik.com/aspnet-ajax/grid/examples/generalfeatures/columntypes/defaultcs.aspx

    All the best,
    Nikolay
    the Telerik team
    Browse the vast support resources we have to jump start your development with RadControls for ASP.NET AJAX. See how to integrate our AJAX controls seamlessly in SharePoint 2007/2010 visiting our common SharePoint portal.
  7. Vyas
    Vyas  avatar
    6 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    Yes thats true.

    But EditDataFormatString is not supporting in my grid.

    That is the only property which I would have missed.

    I added this and its throwing error.

    Whats wrong with my Grid?

    Regards,
    Vyas
  8. Vyas
    Vyas  avatar
    6 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    please reply
  9. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 10 Feb 2011 Link to this post

    Hello Vyas,
    Since the EditDataFormatString is not available for the version that you are using, you need to set it explicitly. Here is the sample code.
     aspx:
    <telerik:GridDateTimeColumn FilterControlWidth="120px" DataField="BirthDate" HeaderText="MyDate"
        SortExpression="DateAdded" DataType="System.DateTime" UniqueName="MyDate" PickerType="DatePicker"
        DataFormatString="dd,MMM,yyyy" HtmlEncode="false" ><HeaderStyle Width="160px" />
    </telerik:GridDateTimeColumn>

    C#:
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
     {
     if (e.Item is  GridEditFormItem && e.Item.IsInEditMode)
       {
         GridEditFormItem item = (GridEditFormItem)e.Item;
         RadDatePicker DatePicker = (e.Item as GridEditFormItem)["MyDate"].Controls[0] as RadDatePicker;
         string dateformat = (RadGrid1.MasterTableView.GetColumn("MyDate") as  GridDateTimeColumn).DataFormatString;
         DatePicker.DateInput.DateFormat = dateformat;
       }
     }

    Thanks,
    Shinu.
  10. Vyas
    Vyas  avatar
    6 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    Hi Shinu,

    I am sorry to say that the code u have given is still not resolving my issue. I don't know why.

    Do you mind in suggesting  me any other solution.

    Regards,
    Vyas
  11. Vyas
    Vyas  avatar
    6 posts
    Member since:
    Oct 2010

    Posted 10 Feb 2011 Link to this post

    Hi,

    Thank you so much for your wonderful support. I cracked the issue and the solution is like this: Snippet
    <%# Eval("DateAdded""{0: MMM. dd, yyyy}")%>
    
    
    This solves the issue. Give the  format string in the Eval itself. It works.
    
    Regards,
    Vyas
  12. John
    John avatar
    1 posts
    Member since:
    Sep 2012

    Posted 06 Sep 2012 Link to this post

    I have the same problem with formating DateTime and absolutely nothing works to fix it. The Eval seems more like a work around than a good solution.

  13. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 11 Sep 2012 Link to this post

    Hello John,

    Could you please elaborate a bit more on your scenario? Are you using a GridDateTimeColumn  or GridTemplateColumn with placed RadDateTimePicker into its templates. Also it will be helpful if you post your aspx markup code with the related code behind file. Thus we will be able to get more information about your scenario and provide you more to the point answer.

    Looking forward for your reply.
     
    Kind regards,
    Radoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  14. HammeTech
    HammeTech avatar
    3 posts
    Member since:
    Mar 2014

    Posted 11 Aug 2014 Link to this post

    I was having this same issue on one of the RadGrids within my site; however, others worked fine.  I attempted many of the suggestions above (except using the "Eval" statement, so I cannot whether or not it would have worked for me); however, none of the suggestions corrected the issue.  So, I started comparing the RadGrid that worked to the RadGrid that did not work and the difference that I found was in the definition of the columns within my DataTable that I was using as the DataSource for the RadGrid.  For the DataTable that was working, the DataColumn was set to a DataType of "System.DateTime"; however for the DataTable that was not working, the DataType for the DataColumn was "System.String".  I modified my code to create the DataColumn with a DataType of "System.DateTime" and that corrected the formatting issue.  (NOTE: my Currency columns were not formatting correctly at first either)

    Here is sample code that worked for me:

    Markup for RadGrid:
    <telerik:RadGrid ID="rgTransactions" runat="server" Skin="WebBlue">
        <MasterTableView AutoGenerateColumns="false" NoMasterRecordsText="No debits or credits have been made." >
            <Columns>
                <telerik:GridBoundColumn DataField="TranDate" HeaderText="Date" UniqueName="TranDate" DataFormatString="{0:d}" />
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description" />
                <telerik:GridBoundColumn DataField="Debit" HeaderText="Debit" UniqueName="Debit" DataFormatString="{0:C}" />
                <telerik:GridBoundColumn DataField="Credit" HeaderText="Credit" UniqueName="Credit" DataFormatString="{0:C}" />
                <telerik:GridBoundColumn DataField="Balance" HeaderText="Balance" UniqueName="Balance" DataFormatString="{0:C}" />
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    C# code to create DataTable:
    DataTable balanceTable = new DataTable();
    balanceTable.Columns.Add("TranDate", typeof(DateTime));
    balanceTable.Columns.Add("Description");
    balanceTable.Columns.Add("Debit", typeof(decimal));
    balanceTable.Columns.Add("Credit", typeof(decimal));
    balanceTable.Columns.Add("Balance", typeof(decimal));

    Pay particular attention to the typeof() in the Columns.Add, that was the key to making the formatting work for my situation.
  15. Brandon R.
    Brandon R. avatar
    2 posts
    Member since:
    Dec 2014

    Posted 18 Dec 2014 in reply to HammeTech Link to this post

    Yes - THIS.

    Turns out that due to my custom solution with dynamic columns, I had to convert my ViewModel into a DataTable for flexibility (as opposed to the usual reflective way that the grid reads your data).  After a long time of playing with it, I found that explicitly setting the DataColumn's DataType fixed everything.

    Thank you, sir.
  16. claire
    claire avatar
    1 posts
    Member since:
    Oct 2015

    Posted 02 Nov 2015 Link to this post

    Hi Support Team,

     

    On the above posted a working solution by Vyas like below, could you provide further details on how/where to implement this Eval (in aspx /in C#)?

     

    <%# Eval("DateAdded", "{0: MMM. dd, yyyy}")%>

     

     Regards,

    Claire

     

     

     

     

     

     

  17. Radoslav
    Admin
    Radoslav avatar
    1564 posts

    Posted 05 Nov 2015 Link to this post

    Hi Claire,

    Eval() is asp.net method which is used for one way binding to the data. It can be used in both markup code and code behind. More infroamtion you can find here:
    http://stackoverflow.com/questions/1793274/what-is-the-use-of-eval-in-asp-net
    https://msdn.microsoft.com/en-us/library/4hx47hfe%28v=vs.110%29.aspx
    http://forums.asp.net/t/1097781.aspx?+EVAL+vs+BIND

    I hope this helps.

    Regards,
    Radoslav
    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
UI for ASP.NET Ajax is Ready for VS 2017