Geeks With Blogs

News Awarded Microsoft MVP C#.NET - 2007, 2008 and 2009

I am born in Bangladesh and currently live in Melbourne, Australia. I am a Microsoft Certified Application Developer MCAD Chartered Member (C# .Net)and born in Bangladesh.
I am founder and Chief Executive Officer of
Simplexhub, a highly experienced software development company based in Melbourne Australia and Dhaka, Bangladesh. Co-founder and core developer of Pageflakes
Simplexhub, is on its mission to build a smart virtual community in Bangladesh and recently launched beta an ASP.NET MVC application written in C#.NET.

Some of My Articles
Flexible and Plugin based .Net Application..
Mass Emailing Functionality with C#, .NET 2.0, and Microsoft® SQL Server 2005 Service Broker'
Write your own Code Generator or Template Engine in .NET

Shahed Khan blog

Someone asked me today:
I have a text field and a drop down menu, based on the values of these 2 when i click on a button an api call is made to get the results. Now i want to display these results in a ListView using GridView. How can i use ObservableCollection to read the data when the search button is hit.

My Short Reply:

Creating an ObservableCollection is pretty straight forward.

1. For example say we have class Customer { id, name, address }

2. Now lets create a DataSrc that returns an ObservableCollection of Customer
public class CustomerDataSrc
 private ObservableCollection<Customer> _results = new
 public ObservableCollection<Customer> Customers {get { return _results; }}
 private void LoadCustomers()
   IList<Customer> customers = YourDAL.FindAll();
   foreach (Customer customer in customers)

3. Declare an ObjectDataProvider in your XAML Page.Resources
 <ObjectDataProvider x:Key="CustomerDataSrc"
                   ObjectType="{x:Type Client_DataSources:CustomerDataSrc}"/>

4. Bind the ListView like this:
<ListView x:Name="dataGrid"
              ItemsSource="{Binding Path=Customers,
              Source={StaticResource CustomerDataSrc}}">

5. In the code behind do this: (change according to the on button click event)

private void Page_Loaded(object sender, RoutedEventArgs e)
  ObjectDataProvider odp = this.FindResource("CustomerDataSrc") as ObjectDataProvider;
  _customerSrc = odp.ObjectInstance as CustomerDataSrc;
  _customerSrc.IsDesignTime = System.ComponentModel.DesignerProperties.GetIsInDesignMode(this);

Hope this helps

Posted on Wednesday, October 10, 2007 1:08 PM | Back to top

Comments on this post: Databinding using ObservableCollection<T>

# re: Databinding using ObservableCollection<T>
Requesting Gravatar...
Great..It really helps..
Left by Asem on Feb 07, 2008 1:48 AM

# re: Databinding using ObservableCollection<T>
Requesting Gravatar...
What a Fuckall explaination...
Left by Ghanta Singh on Aug 02, 2009 3:48 PM

Your comment:
 (will show your gravatar)

Copyright © Shahed Khan | Powered by: | Join free