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

DataFormatString Not working

15 Answers 1078 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Avidan
Top achievements
Rank 1
Avidan asked on 16 Nov 2009, 04:56 PM

 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

 

 

 

 

 

 

 

15 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 17 Nov 2009, 07:00 AM
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.
0
Vyas
Top achievements
Rank 1
answered on 10 Feb 2011, 08:19 AM
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.

0
Vyas
Top achievements
Rank 1
answered on 10 Feb 2011, 08:28 AM
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
0
Nikolay Rusev
Telerik team
answered on 10 Feb 2011, 08:29 AM
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.
0
Vyas
Top achievements
Rank 1
answered on 10 Feb 2011, 08:36 AM
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
0
Vyas
Top achievements
Rank 1
answered on 10 Feb 2011, 09:36 AM
please reply
0
Shinu
Top achievements
Rank 2
answered on 10 Feb 2011, 10:41 AM
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.
0
Vyas
Top achievements
Rank 1
answered on 10 Feb 2011, 11:14 AM
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
0
Vyas
Top achievements
Rank 1
answered on 10 Feb 2011, 02:05 PM
Hi,

Thank you so much for your wonderful support. I cracked the issue and the solution is like this:
<%# Eval("DateAdded""{0: MMM. dd, yyyy}")%>


This solves the issue. Give the  format string in the Eval itself. It works.

Regards,
Vyas
0
John
Top achievements
Rank 1
answered on 06 Sep 2012, 09:08 PM
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.

0
Radoslav
Telerik team
answered on 11 Sep 2012, 11:58 AM
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.
0
HammeTech
Top achievements
Rank 2
answered on 11 Aug 2014, 12:51 PM
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.
0
Brandon R.
Top achievements
Rank 1
answered on 18 Dec 2014, 10:18 PM
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.
0
claire
Top achievements
Rank 1
answered on 03 Nov 2015, 05:34 AM

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

 

 

 

 

 

 

0
Radoslav
Telerik team
answered on 05 Nov 2015, 06:35 AM
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
Tags
Grid
Asked by
Avidan
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Vyas
Top achievements
Rank 1
Nikolay Rusev
Telerik team
John
Top achievements
Rank 1
Radoslav
Telerik team
HammeTech
Top achievements
Rank 2
Brandon R.
Top achievements
Rank 1
claire
Top achievements
Rank 1
Share this question
or