Posted by: Cirilo Meggiolaro | 03/11/2009

Tip of the day #148 – ObservableCollection

The generic ObservableCollection provides an engine that generates notifications when data is added, removed or when the collection is refreshed. The collection is really useful especially when you bind the collection to UI controls such as ListBox, DataGridView and so on.

How to…

Let’s check today how to create an application that displays a DataGridView control that is refreshed every time some item is added to or removed from the ObservableCollection object.

  • Create a new Windows Forms application;
  • Add a reference to WindowsBase.dll;
  • Add an using statement to System.Collections.ObjectModel;
  • Let’s create a class from Customer type as the following code describes:

public class Customer
    public int ID { get; set; }
    public string Name { get; set; }
    public DateTime DOB { get; set; }

  • On the form’s code behind, let’s create a private ObservableCollection object of Customers:

private ObservableCollection<Customer> _customers = new ObservableCollection<Customer>();

  • Add an event handler for the System.Collections.Specialized.CollectionChanged event on the form constructor so a notification is generated by the collection and we can refresh the form:

_customers.CollectionChanged += new NotifyCollectionChangedEventHandler(_customers_CollectionChanged);

  • Add a DataGridView to the form and add three columns: colID, colName and colDOB. Set the DataPropertyName property to ID, Name and DOB respectively;
  • Add some customer items to the customers collection and bind it to the data grid view control:

_customers.Add(new Customer() { ID = 1, Name = “Bill Smith”, DOB = new DateTime(1979, 4, 23) });
_customers.Add(new Customer() { ID = 2, Name = “Will Mayers”, DOB = new DateTime(1973, 7, 18) });
_customers.Add(new Customer() { ID = 3, Name = “George Smithson”, DOB = new DateTime(1982, 12, 12) });
_customers.Add(new Customer() { ID = 4, Name = “Max Ng”, DOB = new DateTime(1967, 8, 2) });

dataGridView1.DataSource = _customers;

  • Add a button to the form. Create an event handler to the click event that will be used to add a new item to the _customers collection:

private void btnInsert_Click(object sender, EventArgs e)
    if (_customers != null)
        _customers.Add(new Customer() { ID = 5, Name = “One more item”, DOB = new DateTime1970, 5, 4) });

  • Add a second button to the form used to remove items from the collection:

private void btnRemove_Click(object sender, EventArgs e)
    if ((_customers != null) && (_customers.Count > 0))
        _customers.RemoveAt(_customers.Count – 1);

  • Let’s add the code to our CollectionChanged event so we can refresh the data grid when an item is added or removed from the collection.

private void _customers_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    dataGridView1.DataSource = null;
    dataGridView1.DataSource = _customers;


  1. Very useful collection!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: