Keep OldValue when cell is not valid in CellValidating event

2 posts, 0 answers
  1. Patryk
    Patryk avatar
    6 posts
    Member since:
    Jul 2013

    Posted 30 Oct 2013 Link to this post

    I want to lose focus and keep e.OldValue if cell is not valid in CellValidating event. Similar behavior to pressing ESC.
    I wrote this code:

    private object _cachedOldValue;
    private bool _isValidValue;
     
    private void radGridViewIssues_CellValidating(object sender, CellValidatingEventArgs e)
    {
        _isValidValue = true;
        RadGridView radGridView = sender as RadGridView;
     
        if (radGridView == null || !radGridView.IsInEditMode)
        {
            return;
        }
     
        ValidationResult validationResult = _presenter.ValidateCurrentValue(e.Column.Name, e.Value);
     
        if (!validationResult)
        {
            _isValidValue = false;
            _cachedOldValue = e.OldValue;
        }
    }
     
    private void radGridViewIssues_CellValidated(object sender, CellValidatedEventArgs e)
    {
        RadGridView radGridView = sender as RadGridView;
     
        if (radGridView == null)
        {
            return;
        }
     
        if (!_isValidValue)
        {
            radGridView.CurrentCell.Value = _cachedOldValue;
        }
    }
    My column has float? datatype. It works with digits, but throws an exception, when I write some letters.

    How can I achieve the desired functionality?
  2. Dess | Tech Support Engineer, Sr.
    Admin
    Dess | Tech Support Engineer, Sr.  avatar
    3945 posts

    Posted 04 Nov 2013 Link to this post

    Hello Patryk,

    Thank you for contacting Telerik Support.

    In order to achieve your goal, please have a look at the following code snippet:
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
     
            List<MyObject> list = new List<MyObject>();
     
            for (int i = 0; i < 10; i++)
            {
                list.Add(new MyObject(i,"Title" + i,1.5f * (i + 1)));
            }
     
            this.radGridView1.AutoGenerateColumns = false;
     
            GridViewTextBoxColumn idBoxColumn = new GridViewTextBoxColumn("ID");
            this.radGridView1.MasterTemplate.Columns.Add(idBoxColumn);
            GridViewTextBoxColumn titleBoxColumn = new GridViewTextBoxColumn("Title");
            this.radGridView1.MasterTemplate.Columns.Add(titleBoxColumn);
            GridViewTextBoxColumn priceBoxColumn = new GridViewTextBoxColumn("Price");
            this.radGridView1.MasterTemplate.Columns.Add(priceBoxColumn);
     
            this.radGridView1.DataSource = list;
     
            //register the custom row selection behavior
            BaseGridBehavior gridBehavior = this.radGridView1.GridBehavior as BaseGridBehavior;
            gridBehavior.UnregisterBehavior(typeof(GridViewDataRowInfo));
            gridBehavior.RegisterBehavior(typeof(GridViewDataRowInfo), new CustomGridBehavior());
        }
     
        public class CustomGridBehavior : GridDataRowBehavior
        {
            protected override bool ProcessEnterKey(KeyEventArgs keys)
            {
                if (this.GridControl.IsInEditMode &&
                    !ValidateCurrentValue(this.GridControl.CurrentColumn.Name,
                        this.GridControl.ActiveEditor.Value))
                {
                    this.GridViewElement.CancelEdit();
                }
                return base.ProcessEnterKey(keys);
            }
     
            public bool ValidateCurrentValue(string columnName, object currentValue)
            {
                bool isValid = true;
     
                // Perform validation logic here and set isValid to true or false.
                if (columnName == "Price")
                {
                    float val;
                    if (currentValue != string.Empty && !float.TryParse(currentValue.ToString(), out val))
                    {
                        isValid = false;
                    }
                }
     
                return isValid;
            }
        }
         
        public class MyObject
        {
            public int ID { get; set; }
     
            public string Title { get; set; }
     
            public float? Price { get; set; }
             
            public MyObject(int iD, string title, float? price)
            {
                this.ID = iD;
                this.Title = title;
                this.Price = price;
            }
        }
    }

    Note that the Price column allows an empty value or a valid float value when ending edit mode with pressing Enter key. All string values are ignored and the old value will be returned in the cell.

    I hope this information helps. Should you have further questions, I would be glad to help.

    Regards,
    Desislava
    Telerik
    TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
    Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
    Sign up for Free application insights >>
Back to Top