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

Binding to DynamicObjects and validation

3 Answers 174 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Goran
Top achievements
Rank 1
Goran asked on 01 Aug 2012, 02:31 PM
Hi,

I have a GridView bound to a ObservableList<DynamicObject>. I need to use DynamicObject since there could be undetermined number of columns displayed in GridView. The binding to data works fine.

I would also like to notify user which rows are containing invalid data. When using regulkar types and DataAnnotations everything works fine. How can I achieve the same result with DynamicObject?

Only requirement is if data exists or not. All data are of string type, If required for UI validation, I can expose bool properties (like IsValid)

public class SomeObject : DynamicObject, INotifyPropertyChanged
{
    public bool IsValid { ... }
}


Regards,
Goran

3 Answers, 1 is accepted

Sort by
0
Accepted
Nedyalko Nikolov
Telerik team
answered on 03 Aug 2012, 11:06 AM
Hi,

RadGridView supports System.ComponentModel.DataAnnotations.IValidatableObject interface, so you can achieve your goals with a few lines of code.
I'm pasting my sample DynamicPerson class that works fine on my end (I've just tested it).

public class DynamicPerson : DynamicObject, IValidatableObject
    {
        private Dictionary<string, object> data;
        public Dictionary<string, object> Data
        {
            get
            {
                if (this.data == null)
                {
                    this.data = new Dictionary<string, object>();
                }
                return this.data;
            }
        }
 
        public override IEnumerable<string> GetDynamicMemberNames()
        {
            return this.Data.Keys;
        }
 
        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            if (this.Data.ContainsKey(binder.Name))
            {
                this.Data[binder.Name] = value;
            }
            else
            {
                this.Data.Add(binder.Name, value);
            }
            return true;
        }
 
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            return this.Data.TryGetValue(binder.Name, out result);
        }
 
        #region IValidatableObject Members
 
        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (this.Data.ContainsKey("FirstName"))
            {
                if (string.IsNullOrEmpty(this.Data["FirstName"].ToString()))
                {
                    return new List<ValidationResult>()
                    {
                        new ValidationResult("First name is required!!!", new List<string>(){"FirstName"})
                    };
                }
            }
 
            return null;
        }
 
        #endregion
    }

Kind regards,
Nedyalko Nikolov
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

0
Goran
Top achievements
Rank 1
answered on 03 Aug 2012, 11:34 AM
Thanks Nedyalko, I totally forgot about IValidatableObject, I need to start to take notes. :)

Regards,
Goran
0
Joel Palmer
Top achievements
Rank 2
answered on 26 Aug 2013, 04:10 PM
Note:  IValidatableObject isn't part of Silverlight.
Tags
GridView
Asked by
Goran
Top achievements
Rank 1
Answers by
Nedyalko Nikolov
Telerik team
Goran
Top achievements
Rank 1
Joel Palmer
Top achievements
Rank 2
Share this question
or