This is a migrated thread and some comments may be shown as answers.

Keep OldValue when cell is not valid in CellValidating event

1 Answer 85 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Patryk
Top achievements
Rank 1
Patryk asked on 30 Oct 2013, 12:08 PM
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?

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 04 Nov 2013, 11:01 AM
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 >>
Tags
GridView
Asked by
Patryk
Top achievements
Rank 1
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or