Solved

Fill ObservableCollection with database query

Posted on 2012-03-22
3
2,911 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 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