Articles in this section
Category / Section

How to set MaxLength to the UWP 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

Conclusion

I hope you enjoyed learning about how to set MaxLength to the UWP GridColumns.

You can refer to our UWP DataGrid feature tour page to know about its other groundbreaking feature representations. You can also explore our UWP DataGrid documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

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