Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3098
  • Last Modified:

Fill ObservableCollection with database query

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
sr2007
Asked:
sr2007
1 Solution
 
ambienceCommented:
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
 
Sathish DVSoftware EngineerCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now