How To Validate The Text In Edit Mode Of Gridtextcolumn In Datagrid(Sfdatagrid)?

Sample date Updated on Sep 13, 2025
columns datagrid validation wpf

About the sample

This example illustrates how to validate the text in edit mode of GridTextColumn in WPF DataGrid (SfDataGrid)?

WPF DataGrid (SfDataGrid) does not provide the direct support for validating the text in edit mode in GridTextBoxColumn. You can achieve this by customizing the GridCellTextBoxRenderer class. In this class you can get the old value and new value from KeyDown and KeyUp events.

public class GridCellTextBoxRendererExt : GridCellTextBoxRenderer
{
       string oldvalue = null;
       string newvalue = null;

       public override void OnInitializeEditElement(DataColumnBase dataColumn, TextBox uiElement, object dataContext)
       {
           base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
           uiElement.KeyDown += UiElement_KeyDown;
           uiElement.KeyUp += UiElement_KeyUp;
       }

       private void UiElement_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
       {
           TextBox textBox = (e.OriginalSource as TextBox);
           newvalue = textBox.Text;
           Console.WriteLine("NewValue: " + newvalue + "\n\noldvalue: " + oldvalue);
           if (newvalue == "Sample")
           {
               textBox.Text = oldvalue;
               textBox.SelectionStart = textBox.Text.Length;
           }
       }

       private void UiElement_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
       {
           oldvalue = (e.OriginalSource as TextBox).Text;
       }
}

KB article - How to validate the text in edit mode of GridTextColumn in DataGrid(SfDataGrid)?

Requirements to run the demo

Visual Studio 2015 and above versions

Up arrow