Telerik problems with XML data source bindings

Source: Internet
Author: User

Telerik GridView default XElement data source direct binding, will cause the built-in sort, filter, group and other functions can not be used.

The reason is that the functions of the Telerik GridView are implemented according to the data type, while the XElement binding is indistinguishable from the data source itself.

One solution is to convert the properties of the binding to specific properties, that is, the Telerik official website so-called various datasource XML binding method

Another solution for the actual project is to turn the data source into a dynamic data source, and then bind the Itemsource to the Dynamic Data source.
There might be other ways, too, I didn't test it out.

The code is as follows:

public void LoadXMLData (XElement data)        {            if (data! = null)            {this                . Items = new Observablecollection<dynamic> (from element in data. Elements () Select New DataRow (todictionary (Element), Element));            }        }
   Private idictionary<string, object> todictionary (XElement Element)        {            var dict = new Dictionary<string , object> ();            foreach (var e in element. Elements ())            {                dict. ADD (E.name.localname, E.value);            }            return dict;        }

Add a class that transforms the dynamic:

 public class Datarow:dynamicobject, inotifypropertychanged {readonly idictionary<string, object> data;        Public XElement Itemxml {get; set;}            Public DataRow (idictionary<string, object> source,xelement item) {data = source;        item = Item; } public override ienumerable<string> Getdynamicmembernames () {return data.        Keys; } public override bool Trygetmember (GetMemberBinder binder, out object result) {result = This[bi NDEr.            Name];        return true; } public override bool Trysetmember (SetMemberBinder Binder, Object value) {This[binder.            Name] = value;        return true; } public Object This[string ColumnName] {get {if (data.                ContainsKey (ColumnName)) {return data[columnname];  } return null;          } set {if (!data. ContainsKey (ColumnName)) {data.                    ADD (columnName, value);                OnPropertyChanged (ColumnName);                        } else {if (Data[columnname]! = value) {                        Data[columnname] = value;                    OnPropertyChanged (ColumnName);  }}}} private void OnPropertyChanged (String propertyname) {if            (propertychanged! = null)            {propertychanged (this, new PropertyChangedEventArgs (PropertyName));        }} #region INotifyPropertyChanged members public event PropertyChangedEventHandler PropertyChanged; #endregion}

  

Telerik problems with XML data source bindings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.