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

DataTable and INotifyPropertyChanged

2 Answers 150 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jingmeng
Top achievements
Rank 1
Jingmeng asked on 23 May 2011, 11:33 PM
hey all,
i'm working on a grid, to be simple, which has 3 columns, price, quantity, and total, total is price*quantity, i know, it working great if i have gridview.itemsource is binding with a class which with INotifyPropertyChanged.  but so far, i need binding with a datatable ( Enumerable(Dictionary<string, object>) ), so i have such problem, when i change quantity or price, i can update that datatable (change price or quantity and change total also) by using celledited event, but the value in total cell didnt update, and either because im in celledited event, i cant do grid.itemsource = null; grid.itemsource = datatable; 

so basically, is possible put  INotifyPropertyChanged in datatable? or there is any other solution..

cheers

2 Answers, 1 is accepted

Sort by
0
Milan
Telerik team
answered on 24 May 2011, 09:34 AM

Hi Jingmeng,

You could use a DataView instead which supports InotifyCollectionChanged and InotifyhPropertyChange events. Simply use myDataTable.DefaultView as ItemsSource.



Regards,
Milan
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jingmeng
Top achievements
Rank 1
answered on 24 May 2011, 10:45 PM
hey milan,
can i use system.data.datatable in silverlight? cause the one im use is light-weight datatable
like 
public class DataTable : IEnumerable
{
    List<DataColumn> columns = null;
    public List<DataColumn> Columns
    {
        get
        {
            if (columns == null)
            {
                columns = new List<DataColumn>();
            } 
            return columns;
        }
    }
 
    List<DataRow> rows = null;
    public List<DataRow> Rows
    {
        get
        {
            if (rows == null)
            {
                rows = new List<DataRow>();
            }             
            return rows;
        }
    }
 
    public object NewRow()
    {
        if (queryable != null)
        {
            return Activator.CreateInstance(queryable.ElementType);
        }
 
        return null;
    }
 
    #region IEnumerable Members
    IQueryable queryable = null;
    public IEnumerator GetEnumerator()
    {
        if (queryable == null)
        {
            var type = ClassFactory.Instance.GetDynamicClass(this.Columns.Select(c => new System.Linq.Dynamic.DynamicProperty(c.ColumnName, c.DataType)));
            var propertyInfos = type.GetProperties().ToList();
 
            var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(type));
            foreach (var row in this.Rows)
            {
                var item = Activator.CreateInstance(type);
                propertyInfos.ForEach(p => p.SetValue(item, row[p.Name], null));
 
                list.Add(item);
            }
 
            queryable = list.AsQueryable();
        }
 
        return queryable.GetEnumerator();
    }
 
    #endregion
 
    public IList ToList()
    {
        var enumerator = GetEnumerator();
        var list = (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(queryable.ElementType));
        while (enumerator.MoveNext())
        {
            list.Add(enumerator.Current);
        }
        return list;
    }
 
    static Type GetNonNullableType(Type type)
    {
        return IsNullableType(type) ? type.GetGenericArguments()[0] : type;
    }
 
    static bool IsNullableType(Type type)
    {
        return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);
    }
}
 
public class DataColumn
{
    public DataColumn()
    {
        DataType = typeof(object);
    }
 
    public Type DataType { get; set; }
    public string ColumnName { get; set; }
}
 
public class DataRow : Dictionary<string, object>
{
 
}

cheers

MENG
Tags
GridView
Asked by
Jingmeng
Top achievements
Rank 1
Answers by
Milan
Telerik team
Jingmeng
Top achievements
Rank 1
Share this question
or