Articles in this section

How to validate the text in edit mode of GridTextColumn in WPF DataGrid (SfDataGrid)?

WPF DataGrid (SfDataGrid) does not provide the support for validating the text in edit mode for  GridTextBoxColumn. You can achieve this by customizing the GridCellTextBoxRenderer.

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;
        }
}

 

Validating text in SfDataGrid

View WPF DataGrid Editing Demo in GitHub

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