Skip to main content Skip to footer

Export Translated Text for DataGridTemplateColumn

This article on C1DataGrid for Silverlight is a utility blog to cater to the requirement of the developers who would like to show Translated text for cell values in **DataGridTemplateColumn** and along with that, export these translated Text to an excel sheet. Nothing fancy about the solution. Implementation can be summarized in two basic module.

  1. Use IValueConverter interface to show the translated Text in the cell.
  2. Use GettingCellValue event to export the desired values from Template Column.

Consider a scenario where you have a Boolean column in your datasource. When you bind this field to C1DataGrid, it shows up with a CheckBox in the cell. However, your requirement might be to show String values. Alternatively, you can bind this field to a DataGridTextColumn or use DataGridTemplateColumn with a TextBox. When you use this solution, values are shown in string format as 'True' or 'False'. Now you may have another requirement to show string 'Yes' or 'No'. This is where you need to use IValueConverter. Following Code block shows the XAML Binding of the Columns and the use of IValueConverter. XAML --------


<UserControl.Resources>  
<local:BoolTextConverter x:Key="boolConv"/>  
</UserControl.Resources>  

<c1grid:C1DataGrid Grid.Row="1" x:Name="_dataGrid" CanUserAddRows="False" Height="276" AutoGenerateColumns="False"  
ItemsSource="{Binding}" HeadersVisibility="Column">  
   <c1grid:C1DataGrid.Columns>  
      <c1grid:DataGridBoundColumn Header="First Name" Binding="{Binding FirstName}"/>  
      <c1grid:DataGridBoundColumn Header="Last Name" Binding="{Binding LastName}"/>  
      <c1grid:DataGridTemplateColumn Header="Template Bool" GettingCellValue="DataGridTemplateColumn_GettingCellValue">  
         <c1grid:DataGridTemplateColumn.CellTemplate>  
            <DataTemplate>  
               <TextBlock Text="{Binding Checked, Converter={StaticResource boolConv}, Mode=TwoWay}"/>  
            </DataTemplate>  
         </c1grid:DataGridTemplateColumn.CellTemplate>  
      </c1grid:DataGridTemplateColumn>  
   </c1grid:C1DataGrid.Columns>  
</c1grid:C1DataGrid>  

C# Code -----------


public class BoolTextConverter:IValueConverter  
{  
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    {  
       if (value.ToString() == "False")  
         return "No";  
       else  
         return "Yes";  
    }  

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)  
    {  
       return value;  
    }  
}  


Once you have the translated text in the cell, and you export the grid data into an excel sheet, Column for the translated values appear blank for DataGridTemplateColumn. This is dues to the reason that DataGridTemplateColumn does not export the values on its own. You have to explicitly mention the value to be exported. This declaration has to be done in the event GettingCellValue. Given code block shows the functionality for the event.


private void DataGridTemplateColumn_GettingCellValue(object sender, DataGridGettingCellValueEventArgs e)  
{  
   e.Value = ((System.Windows.Controls.TextBlock)(e.Row.Presenter[_dataGrid.Columns[4]].Content)).Text;  
}  

In the above code snippet, I have used the 'Text' property. This property will export the translated text value (Yes/No) instead of the actual value (True/False) for the cells. Third Column in the given image shows the desired implementation to show translated value (Yes/No). Download the attached samples for complete implementation. Sample C# Sample VB.Net

MESCIUS inc.

comments powered by Disqus