HomeSilverlight Silverlight 4.0 Data Manipulation and Navi...
Silverlight 4.0 Data Manipulation and Navigation using CollectionViewSource with WCF RIA Services
This article explains the navigation features available in “CollectionViewSource.” We will also cover CRUD (Create, Read, Update and Delete) operations using “CollectionViewSource.”
My previous article introduced “CollectionViewSource” in Silverlight 4.0. You can check it out here.
To make this article simple, I managed to create a simple Silverlight 4.0 application which consumes a WCF RIA Service created using the WCF RIA Service Library. If you are not familiar with developing applications using the WCF RIA Service Library and Silverlight 4.0, check out a beginner’s article.
The solution was developed using Microsoft Visual Studio 2010 Ultimate Edition with Microsoft Silverlight 4.0 on Windows 7 Ultimate edition. I didn’t really test it in any other environment. I request that you post in the discussion area if you have any problems with execution.
Navigating through data using CollectionViewSource in Silverlight: screen design
Way back in Visual Basic 6.0, we used to have RecordSet. It used to have the following methods: MoveNext, MovePrevious, MoveLast and MoveFirst. Those methods are mainly used to maintain the “current” object in memory so that it could be bound to controls on the form. We can use “CollectionViewSource” to make use of such functionality.
This section explains the code in the previous section. The following is the code which declares “CollectionViewSource”:
<UserControl.Resources>
<CollectionViewSource x:Key="cvsEmp"/>
</UserControl.Resources>
All of the textboxes are declared with “Two-Way” binding (with the respective fields) so that they will pull and push data into the “CollectionViewSource.” You can observe the declarations below:
All of the above textboxes are contained in a Grid layout. The Grid layout acts as a data context for the above list of textboxes. You can observe the definition of the Grid layout as follows:
To display buttons (navigation buttons) adjacent to each other, I used a StackPanel with a horizontal orientation. You can understand the same from the code below:
To change the current context (object being bound to controls) we can use MoveCurrentToNext, MoveCurrentToPrevious, MoveCurrentToFirst or MoveCurrentToLast. All of these methods belong to the “View” object of “CollectionViewSource.”
Once we click on the “First” button, it should show the following result:
In the previous section, we saw how to navigate using “CollectionViewSource.” In this section, we will concentrate on manipulating data using “CollectionViewSource.” I made a couple of modifications to the previous screen design, so that it would look something like the following:
We can search for an object in a “CollectionViewSource” using the following code:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSearch.Click
Adding a new object to “CollectionViewSource” is a simple, indirect process. All you have to do is add a new object to the context, which would automatically appear in “CollectionViewSource.” To reflect the same in the controls, we need to move the “current” of “CollectionViewSource” to the newly added object as follows:
Private Sub btnAddNew_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnAddNew.Click
Dim oEmp As New BusinessLib.Web.emp
oCtxt.emps.Add(oEmp)
oCvsEmp.View.MoveCurrentTo(oEmp)
End Sub
We can use the same technique to delete an object in “CollectionViewSource” as follows:
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnDelete.Click
If Me.oCvsEmp.View.CurrentItem Is Nothing Then
MessageBox.Show("Employee not selected to delete")
Exit Sub
End If
oCtxt.emps.Remove(Me.oCvsEmp.View.CurrentItem)
End Sub
All of the modifications made to the main context (indirectly done using “CollectionViewSource”) can be saved using the following:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnSave.Click
Once you save, the output will look similar to the following:
My next article focuses on more features of the “CollectionViewSource” element. I hope you enjoyed the article; any suggestions, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com