Modifying a DataBound Collection - Page 1
       by kirupa  |  9 December 2007

In my earlier Data Binding to CLR Objects tutorial I described data binding and explained how to use data binding with CLR data. Delving deeper, you learned how to take data from a list and display that data in a listbox control. One of the most common questions I received from that tutorial revolved around adding new data to that list.

On the surface, that seems trivial. Since all of our data is already in the form of a list, simply adding data to that list should automatically update our listbox also. After all, our list is actually an ObservableCollection which is a type of List that implements the INotifyCollectionChanged interface for sending change notifications when content has been added/removed/modified inside the List.

If the above paragraph sums up what you currently feel is the solution to the question, you are correct. That is really all there is to it, but the unclear part is in the details. I mention that all we need to do is add "data to that list." Which list am I specifying? Is it a new instance of our ObservableCollection object in our code-behind file? Is it an existing ObservableCollection instance?

This article aims to answer those questions. Modifying a databound collection is not as trivial as it seems, and there are several correct solutions you can come up with. I will first show you the direct, straight-forward solution and then show you a better, more maintainable approach that would be preferred from a good coding-practices point of view.

Getting Started
To get the most out of this article, it helps greatly to follow along with my instructions. To do that, you will need both Visual C# 2008 Express and Expression Blend 2 installed if you do not already have them. These are the latest versions of the VS Express and Blend products as of the time of this writing:


Download Visual C# 2008 Express
Download Expression Blend 2 Preview (60-Day Trial)

Once you have both of the above products installed, there is one final thing you will need to download. Because this isn't an introductory tutorial teaching you how to accomplish basic tasks, I have already created a sample project for you:


Download Sample Project

Download the above project, extract the files, and open this project in Blend 2. This sample project contains some databound data and a visual interface for allowing you to see how the application looks like. Don't worry -  the code relevant to this article is not included, so you will have to modify this existing project yourself based on my instructions.

Taking a Look at the BindingToPeople Project
The project you downloaded is called BindingToPeople, and when you open it in Blend, you will see our Names application displayed:

Note: You may be asked to Build your solution first, and you can do that by pressing Ctrl + Shift + B or by going to Project | Build Solution.

The goal of this application is to add whatever text you put into the text-field and display it in the listbox. The magic is in how data gets added to the listbox, and that is where data binding comes in. To save you some time, I have already databound our listbox to a CLR data source called PeopleListDS:

[ your Data panel shows you your PeopleListDS collection your listbox uses ]

Beyond the data source and the basic layout of our controls, there is nothing special about the user interface of our application. In the next page, let's look at the code and get a better idea of what this application currently does behind the scenes.

Onwards to the next page!

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.