We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Binding and Formatting Datagrid Values

Hi,

I have an object with several properties which are currently in a list and displayed in a datagrid using Mapping Name on manually defined columns.
For some of these properties such as 'Price' and 'Price Increase' I would like to format the value to have it in percentage or currency format.
Typically for other controls I would bind to the property and use a converter, however I seem to unable to do this as if I try using ValueBinding instead of MappingName and add a converter, the app crashes before the page loads due to using a converter.
Is there a particular method for formatting values in a datagrid?

Regards

Francis  

5 Replies

JG Jai Ganesh S Syncfusion Team September 9, 2016 02:14 PM UTC

Hi Francis, 
 
We have analyzed your query . You can set the format for GridNumericColumn by using the FormatString property like below, 
 
<!--  GridNumericColumn with FomatString  --> 
                <syncfusion:GridNumericColumn Width="160" 
                                              FormatString="C" 
                                              HeaderText="Annual Income" 
                                              MappingName="Income" /> 
 
In the above code, we have displayed the value in Currency format. 
 
Kb Link:  
 
Regards, 
Jai Ganesh S 



FR Francis September 12, 2016 10:22 AM UTC

Hi,

Thanks for your reply.

I noticed there is no culture property available to format the value to a different currency.
Is there a way to standard way to set this or do I have to explicitly set the format string like the last example in the knowledge base article?

Regards

Francis


JN Jayaleshwari N Syncfusion Team September 13, 2016 05:47 AM UTC

Hi Francis, 
 
We have analyzed your query “Culture property to format the value to different currency”. In SfDataGrid and GrdiNumericColumn, we do not have direct property to achieve your requirement.   However, you can achieve your requirement by using CurrentCulture. 

Below code snippet applies Culture format to all the numeric columns in the grid. 

public MainPage() 
{ 
    this.InitializeComponent(); 
    CultureInfo.CurrentCulture = new System.Globalization.CultureInfo("de-DE"); 
} 

You can allow the culture format to specific column by using DisplayBinding converter like in below code snippet. 

Code C#: Converter – format the value to de-DE culture with currency format. 

public class FormatConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, string language) 
    { 
        var columnValue = System.Convert.ToDecimal(value); 
        return columnValue.ToString("C", new System.Globalization.CultureInfo("de-DE").NumberFormat); 
    } 
 
    public object ConvertBack(object value, Type targetType, object parameter, string language) 
    { 
        throw new NotImplementedException(); 
    }       
} 

Code XAML: GridNumericColumn definition with DisplayBinding converter. 
 
  <syncfusion:GridNumericColumn Width="160"  
                                                     DisplayBinding="{Binding Income, Converter={StaticResource formatConverter}}" 
                                              HeaderText="Annual Income"  
                                              MappingName="Income" /> 

In the above code we have displayed currency in de-DE culture. 

Regards, 

Jayaleshwari N. 



FR Francis September 14, 2016 09:31 AM UTC

Hi,

Thank you that works perfectly now.

Regards

Francis


JG Jai Ganesh S Syncfusion Team September 15, 2016 04:51 AM UTC

Hi Francis, 

Thank you for the update. 

Please let us know if you need further assistance on this. 

Regards, 
Jai Ganesh S 


Loader.
Live Chat Icon For mobile
Up arrow icon