Articles in this section
Category / Section

How to set MaxLength to the WinRT GridColumns?

1 min read

You can define MaxLength to the GridColumn in display mode by using DisplayBinding and IValueConverter interface as follows.

C#

public class DisplayBindingConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        //Define maxlength for Column.
        int maxlength = 5;
        //Get the ColumnValue.
        var columnValue = System.Convert.ToString(value);
        if (columnValue.Length < maxlength)
            return columnValue;
        else
            return columnValue.Substring(0, maxlength);
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }
}

 

XAML

  <Syncfusion:GridTextColumn DisplayBinding="{Binding Path=EmployeeName,
                                                     Converter={StaticResource ConverterKey},
                                                     ConverterParameter=EmployeeName}"/>

 

Note: You can use Converter in Value Binding also as follows.

<Syncfusion:GridTextColumn ValueBinding="{Binding Path=EmployeeName,
                                                     Converter={StaticResource ConverterKey},
                                                     ConverterParameter=EmployeeName}"/>

XAML

 

You can also restrict the MaxLength in edit mode by writing Style to TextBox since MS TextBox will be loaded while editing GridTextColumn.

 

Please refer the below code snippet.

XAML

<Window.Resources>
<Style TargetType="TextBox">
            <Setter Property="MaxLength" Value="7" />
        </Style>
</Window.Resources>

 

 

The MaxLength can be set for a particular column by deriving a new class from GridCellTextBoxRenderer and override the OnInitializeEditElement method with the help of TextBox.MaxLength property as follows.

C#

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        // Remove existing TextBox Renderer
        this.sfdatagrid.CellRenderers.Remove("TextBox");
        // Add customized TextBox Renderer.
        this.sfdatagrid.CellRenderers.Add("TextBox", new GridCellTextBoxRendererExt());
    }
}
public class GridCellTextBoxRendererExt : GridCellTextBoxRenderer
{
    public override void OnInitializeEditElement(Syncfusion.UI.Xaml.Grid.DataColumnBase dataColumn, TextBox uiElement, object dataContext)
    {
        if (dataColumn.GridColumn != null && dataColumn.GridColumn.MappingName == "EmployeeName")
        {
            uiElement.MaxLength = 7;
        }
        else
        {
            uiElement.MaxLength = 0;
        }
        base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
    }
}

 

 

Sample Links:

WPF

WinRT

UWP

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