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

[Solved] Can I Use an ICustomFormatter

2 Answers 62 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ric Pullen
Top achievements
Rank 1
Ric Pullen asked on 25 Jun 2009, 08:06 AM
Working in the NHS we have a new directive that the NHS number must be displayed in an xxx xxx xxxx format, but all our current systems store it as an 10 Character string.

I have written a custom formatter, but struggling to find where I can place the code to display the format within the grids display. 
Can this be done and if so how?

Thanks

2 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 29 Jun 2009, 08:42 AM
Hello Ric,

Generally on GridBoundColumn you set DataFormatString which will internally call String.Format(format,value). In your scenario DataFormatString could be as  follow: "{0:### ### ####}"

Other option if you need to apply your custom formatter is to use GridTemplateColumn and in ItemTemplate apply formatting on the value extracted from the item using custom formatter.

On the application attached to this post you will find example how to implement both cases.

Kind regards,
Nikolay
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Ric Pullen
Top achievements
Rank 1
answered on 08 Jul 2009, 12:55 PM
With the help of the previous post I managed to resolve the solution.  As the response from Telerik was regarding formatting numbers, which is already well documented, I had a custom formatter for a string which is not covered so below is the solution I came up with.


If you wish to use a Custom formatter then in order to display this in the radgrid, you will have to do something similar to the example below. (This is server based in order to trap the exception thrown by the custom formatter, writing the code client side would cause the exception to fire and be unhandled)

Server
Protected Function formatNHSNo(ByVal nhsnumber As String) As String
Try
   Return String.Format(New NHSFormatter(), "{0:sp}", nhsnumber)
Catch e As Exception
   Return String.Empty
End Try
End Function

ASPX
<Columns>
    <telerik:GridTemplateColumn HeaderText="NHS Number">
      <ItemTemplate>
           <%#formatNHSNo(Eval("patient_nhs_no"))%>
      </ItemTemplate>
   </telerik:GridTemplateColumn>
</Columns> 

Tags
Grid
Asked by
Ric Pullen
Top achievements
Rank 1
Answers by
Nikolay Rusev
Telerik team
Ric Pullen
Top achievements
Rank 1
Share this question
or