Articles in this section
Category / Section

How to set different cultures for GridDateTimeColumn in the SfDataGrid ?

5 mins read

In the SfDataGrid, the GridDateTimeColumn provides support to display the date time value. You can set different cultures for the GridDateTimeColumn by using the ConverterCulture property in the DisplayBinding and ValueBinding property of that column as shown in the following code example.

XAML

<syncfusion:GridDateTimeColumn DisplayBinding="{Binding Path=OrderDate,
                                                        ConverterCulture=fr-FR,
                                                        Converter={StaticResource converter}}"
                                                        HeaderText="Order Date"
                                                        ValueBinding="{Binding Path=OrderDate,
                                                        ConverterCulture=fr-FR}" />
<syncfusion:GridDateTimeColumn DisplayBinding="{Binding Path=ShippedDate,
                                                        ConverterCulture=en-US,
                                                        Converter={StaticResource converter}}"
                                                        HeaderText="Shipped Date"
                                                        ValueBinding="{Binding Path=ShippedDate,
                                                        ConverterCulture=en-US}" />

 

Two different cultures, fr-FR and en-US are defined to different GridDateTimeColumns. The GridDateTimeColumn value is displayed based on the pattern, by default. So, the ConverterCulture is not considered for the display mode.

Note: By default, short date pattern is applied to the GridDateTimeColumn.

 

To overcome this, you need to return culture formatted column value with the help of the converter in the DisplayBinding as shown in the following code example.

C#

public class Converter:IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

 

Based on the above culture, the GridDateTimeColumn is displayed in the following screenshot.

Figure 1: GridDateTimeColumn with different cultures

The above culture is set in the Display mode of the GridDateTimeColumn when you want to set the same culture format in the edit mode by deriving a new class from the GridCellDateTimeRenderer and overriding the OnInitializeEditElement virtual method.

 The following code example illustrates the override of a new class from the existing GridCellDateTimeRenderer.

C#

public class GridCellDateTimeRendererExt : GridCellDateTimeRenderer
{         
    public override void OnInitializeEditElement(DataColumnBase dataColumn, DateTimeEdit uiElement, object dataContext)
    {
        //Corresponding DateTimeFormat of the ConverterCulture is get from the ValueBinding and assigned to DateTimeEdit, and that is loaded in the edit mode of the GridDateTimeColumn.
        base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
        uiElement.DateTimeFormat = (dataColumn.GridColumn.ValueBinding as Binding).ConverterCulture.DateTimeFormat;
    }     
}

 

The DateTimeFormat is get from the corresponding column’s ValueBinding and assigned the DateTimeEdit UIelement that is loaded in the edit mode of the GridDateTimeColumn. Refer to the following code example to remove the default GridCellDateTimeRenderer and add the customized GridCellDateTimeRendererExt to the renderer’s collection in the SfDataGrid.

C#

public MainWindow()
{
    InitializeComponent();
    //GridCellDateTimeRendererExt custom renderer added to the CellRenderers collection.
    this.sfdatagrid.CellRenderers.Remove("DateTime");
    this.sfdatagrid.CellRenderers.Add("DateTime", new GridCellDateTimeRendererExt());
}

 

Sample Link:

WPF

 

 

 

 

 

 

               

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied