Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
845
Bind Different data source to grid when using data template
posted

Hi

             I am using data template to load controls over map element. I am using XamWebGrid and Chart. I want to bind different data source to grid at different time from DB. How can i do this.  Whether i always need to have a class that extends observablecollection to bind data with that grid. Because i need to bind different type of source object at different time  (let say Student or teacher each will have the different fields). In that case how can i do this.

              I am using XamWebChart also how to bind data to grid and chart at same time.

 

Parents
No Data
Reply
  • 30692
    Suggested Answer
    Offline posted

    Could you provide a sample that illustrates what you are trying to do.

    If you have some enumerable of objects of the same type and you want to put them into an observable collection at runtime, you could do something like this.

     

           public IEnumerable MakeObservable(IEnumerable sourceItems)
            {
                if (sourceItems == null)
                {
                    return null;
                }
                IEnumerator items = sourceItems.GetEnumerator();
                Type itemType = null;
                IList ret = null;
    
                while (items.MoveNext())
                {
                    if (itemType == null)
                    {
                        if (items.Current == null)
                        {
                            continue;
                        }
                        itemType = items.Current.GetType();
                        Type coll = typeof(ObservableCollection<>);
                        coll = coll.MakeGenericType(new[] { itemType });
                        ret = (IList)Activator.CreateInstance(coll);
                    }
    
                    if (ret != null)
                    {
                        ret.Add(items.Current);
                    }
                }
    
                return ret;
            }
    

    Does that help?

    -Graham

Children
No Data