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

Custom Column Value Formatter

2 Answers 146 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Vinoth Kathappanraju
Top achievements
Rank 1
Vinoth Kathappanraju asked on 14 Jun 2016, 05:39 PM

Hi

I have a requirement where i need to pass in a custom formatter for values in a GridViewDataColumn. I looked at DataFormatString but i don't think it satisfies my requirement.My requirement is that i need to round the decimal to 4 decimal places but also show a negative sign if the number is negative. For example, if the number is -0.000001, then my grid should show -0.0000 and not just 0.0000. For a positive number, it should just display 0.0000. How do i get this working? Any help is much appreciated.

 

Thanks

Vinoth

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 16 Jun 2016, 01:32 PM
Hello Vinoth,

To achieve the desired behavior, you will need to set a converter for your binding and handle the rounding manually as, by default, the result you're getting is expected. Here's an example:

public class DecimalToStringConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var number = (decimal)value;
        var shouldAddNegativeSign = number < 0 && number >= -0.00005M;
        var numberAsString = string.Format("{0}{1:F4}", shouldAddNegativeSign ? "-" : "", number);
 
        return numberAsString;
    }
 
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

<telerik:GridViewDataColumn DataMemberBinding="{Binding StadiumCapacity, Converter={StaticResource DecimalToStringConverter}}" />

Would such an approach be suitable for your scenario?

Regards,
Dilyan Traykov
Telerik
Do you need help with upgrading your AJAX, WPF or WinForms project? Check the Telerik API Analyzer and share your thoughts.
0
Vinoth Kathappanraju
Top achievements
Rank 1
answered on 28 Jun 2016, 05:50 PM

Thanks Dilyan. This works great and satisfies my current requirement.

 

Thanks

Vinoth

Tags
GridView
Asked by
Vinoth Kathappanraju
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Vinoth Kathappanraju
Top achievements
Rank 1
Share this question
or