Solved

Fill ObservableCollection with database query

Posted on 2012-03-22
3
2,972 Views
Last Modified: 2013-11-12
I am using Silverlight 4 and SharePoint and want to fill an ObservableCollection from a returned dataset from SharePoint. I have the query and connection to SharePoint done. In my example my List is called _getList and my ListCollectionItems is called _getListItems. My query returns a dataset of x amount of rows of data from an Employee table.

How do I iterate through the returned dataset (_getListItems) and populate Silverlight's ObservableCollection so that I may perform binding and validation on the controls (i.e. textboxes, listboxes, comboboxes, etc.)?
0
Comment
Question by:sr2007
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 22

Expert Comment

by:ambience
ID: 37775346
Using untyped datasets?

Can you change the query to return an Observable collection instead? (if thats in your control)

Alternatively, you may have to create a model class and loop through returned rows to populate the observablecollection. This shouldn't be difficult, however two way binding may require sending updates back to the database?

Check this

http://social.expression.microsoft.com/Forums/en/blend/thread/3772bf00-06a2-466a-8a56-49ed8c5a471b

It shows how to fill a List, but can be changed to return an ObservableCollection instead.
0
 
LVL 4

Accepted Solution

by:
Sathish DV earned 500 total points
ID: 37826917
Dear sr2007,

Since u already have List or ListCollectionItem with entire dataset collection, u can better include one extension method so the List will be returned as ObservableCollection type.

Below is extension method to convert List to Observable collection

public static class ExtensionMethods
    {
        public static ObservableCollection<T> ToObservableList<T>(this IEnumerable<T> data)
        {
            ObservableCollection<T> dataToReturn = new ObservableCollection<T>();

            foreach (T t in data)
                dataToReturn.Add(t);

            return dataToReturn;
        }
    }

So while returning List or ListCollection u can use the below code

Consider ur dataset returns Employee type....

public ObservableCollection<Employee> GetEmp()
{
      return getList.ToObservableList();
}

Let me know if you need any other information.

By doing this u need not to re-write most of the code... just create one Extension method and use it where ever needed.
0

Featured Post

RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question