Blogs

The Progress Bar - DevExpress XPF Blog

Silverlight Grid Control – Data Binding Series – Part 2

     

In my previous post, I introduced the most common ways to bind an AgDataGrid control to a data source and provided a walkthrough of how to data bind using WCF RIA Services.

This is the second post in the data binding series and here, I’ll take a look at how to use a WCF web service and LINQ to SQL classes to bind to the same SQL Server database.

Binding to a SQL Server Express Database using a Web Service and LINQ to SQL Classes

This example is assuming that you have a sample database running on a local SQL Server Express instance (“.\SQLEXPRESS”). If you do not have a database, the one used here will be attached at the end of the post, along with the completed project.

Let’s start by creating a new Silverlight Application Project and a web application that will host it.

New SL Project

Next, right-click the web application project and add LINQ to SQL Classes to the project. Once created, the Object Relational Designer (the O/R Designer) is displayed. You can now establish a connection in the Server Explorer window of Visual Studio and drag and drop the desired tables onto the O/R Designer.

OR Designer - DBML

To make the LINQ class Serializable, click anywhere on the design surface and set the class’ Serialization Mode property to “Unidirectional”.

Now let’s add the WCF Web Service to the project. Implement the interface as follows:

   1: namespace SilverlightGrid.Web
   2: {
   3:     [ServiceContract]
   4:     public interface IService1
   5:     {
   6:         [OperationContract]
   7:         List<Product> GetProducts();
   8:     }
   9: }

The function can then be implemented in the Service1 class using the following code:

   1: public List<Product> GetProducts()
   2: {
   3:     DataClasses1DataContext db = new DataClasses1DataContext();
   4:     var products = from prod in db.Products select prod;
   5:     return products.ToList();
   6: }

Rebuild the solution and add a service reference to the Silverlight application project.

Add Service Reference

Add an AgDataGrid control and bind it using the following code:

   1: using SilverlightGrid.ServiceReference1;
   2:  
   3: namespace SilverlightGrid {
   4:     public partial class MainPage : UserControl {
   5:         public MainPage() {
   6:             InitializeComponent();
   7:             ServiceReference1.Service1Client webService = new ServiceReference1.Service1Client();
   8:             // Sets up an event handler for the method that will be called when the GetCustomersCompleted event is raised. 
   9:             webService.GetProductsCompleted +=
  10:                 new EventHandler<ServiceReference1.GetProductsCompletedEventArgs>(webService_GetProductsCompleted);
  11:             // Makes the asynchronous call. 
  12:             webService.GetProductsAsync();
  13:         }
  14:         private void webService_GetProductsCompleted(object sender, ServiceReference1.GetProductsCompletedEventArgs e) {
  15:             agDataGrid1.DataSource = e.Result;
  16:         }
  17:     }
  18: }

Rebuild and run the application and you’ll be presented with a DevExpress Silverlight Grid Control bound to the Products table of the local SQL Server Express Database.

You can see the video demonstration of this tutorial on the DevExpress Channel: http://tv.devexpress.com/AgGridBindLINQtoSQL.movie

The sample project and SQL database backup have been attached as a single zip file to this post.

Comments

No Comments
More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.