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

value cannot be converted

1 Answer 552 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Thomas
Top achievements
Rank 1
Thomas asked on 04 Feb 2011, 04:49 PM
I created custom column;
It works fine but when I am trying to clear a cell I am geting "Value Cannot be converted" validation error.
My binding is to the nullable double.
Please advise what to do.

thanks

  public class NumericTextBoxColumn :GridViewBoundColumnBase
    {
        public override FrameworkElement CreateCellEditElement(GridViewCell cell, object dataItem)
        {
            this.BindingTarget = NumericTextBox.TextProperty;
            NumericTextBox control = new NumericTextBox();
            control.SetBinding(this.BindingTarget, this.CreateValueBinding());
            return control;
        }

        private Binding CreateValueBinding()
        {
            Binding valueBinding = new Binding();
            valueBinding.Mode = BindingMode.TwoWay;
            valueBinding.NotifyOnValidationError = true;
            valueBinding.ValidatesOnExceptions = true;
            valueBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
            valueBinding.Path = new PropertyPath(this.DataMemberBinding.Path.Path);

            return valueBinding;
        }
    }


where;

   public class NumericTextBox: TextBox
    {
       
        protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
        {
            if(string.IsNullOrEmpty(e.Text))
            {
                e.Handled = true;
                return;
            }
           
            e.Handled = !AreAllValidNumericChars(e.Text);
            base.OnPreviewTextInput(e);
        }

        private bool AreAllValidNumericChars(string str)
        {
            foreach (char c in str)
            {
                if (c == '.' && base.Text.Contains(".")) return false;
                if (!(Char.IsNumber(c) || c=='.' )) return false;
            }

            return true;
        }

    }

1 Answer, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 09 Feb 2011, 04:23 PM
Hi Thomas,

Generally, the exception you are getting is handled internally by the RadGridView (unless you explicitly set it to stop on all exceptions). The reason for it is that converting an empty string to a nullable numeric value cannot be handled correctly. What you may do is to create an IValueConverter that takes care of changing an empty string for example to an empty numeric value.
 

All the best,
Maya
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
Tags
GridView
Asked by
Thomas
Top achievements
Rank 1
Answers by
Maya
Telerik team
Share this question
or