Telerik Telerik
The Telerik Blogs

RadControls for Silverlight Demo with ADO.NET Data Services

Thursday, October 09, 2008 by Valeri Hristov | Comments 9

Yesterday I wrote about how to databind RadTreeView for Silverlight to ADO.NET Data Service and use Load on Demand. The simple application from yesterday was upgraded to a small back-end application for the Northwind database, that allows editing and deleting products. This time I decided to give it to one of our designers and our new Silverlight front end developer and they implemented this slick design:
NorthwindBackEnd

The application is available online:

http://demos.telerik.com/silverlight/northwindbackend/Default.aspx

The source code can be downloaded from here:

NorthwindBackend.zip

In addition to yesterday's application, NorthwindBackend has extended data source, that provides simple undo mechanism and ability to update and delete products in the database through the ADO.NET Data Service. Product updates and deletes are really simple:

public void UpdateSelectedProduct() 
{ 
    if (this.SelectedProduct != null) 
    { 
        Service.UpdateObject(this.SelectedProduct); 
        Service.SetLink(this.SelectedProduct, "Supplier", this.SelectedProduct.Supplier); 
        Service.BeginSaveChanges(null, null); 
    } 
} 

public void DeleteSelectedProduct() 
{ 
    Service.DeleteObject(this.SelectedProduct); 
    Service.BeginSaveChanges((IAsyncResult result) => 
    { 
        SelectedProduct.Category.Products.Remove(SelectedProduct); 
        SelectedProduct = null; 
    }, null); 
} 

 

Since deleting of rows in the database can be dangerous in a real-life application, I demonstrate how to implement simple confirmation, using RadWindow:

private void ButtonDelete_Click(object sender, System.Windows.RoutedEventArgs e) 
{ 
    RadWindow.Confirm("Are you sure you want to delete this product?", 
        (object window, WindowClosedEventArgs args) => 
        { 
            if (args.DialogResult.HasValue && args.DialogResult.Value) 
            { 
                this.DataSource.DeleteSelectedProduct(); 
            } 
        }); 
} 

 

The second parameter of the RadWindow.Confirm method is an event handler, that will be executed when the window closes. There we provide the code that checks whether the user clicked OK or Cancel and then proceed accordingly.

In this application I also modified the generated code for the ADO.NET Data Service in order to change the collections' types. The new thing this time is that I added partial classes that implement INotifyPropertyChanged for each entity. You can find those classes in the Data folder of the NorthwindBackend application. The INotifyPropertyChanged interface is needed because I am using TwoWay bindings to implement the editing functionality of the application without writing code-behind:

<input:RadComboBox ItemsSource="{Binding Suppliers}" 
    SelectedItem="{Binding SelectedProduct.Supplier, Mode=TwoWay}" 
    DisplayMemberPath="ContactName" Height="26" /> 

 

There is one more trick, that I want to show. I am using a converter to show/hide the product properties panel, depending on the SelectedProduct property of the data source:

public class VisibilityNullConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        return value == null ? Visibility.Collapsed : Visibility.Visible; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
        throw new NotImplementedException(); 
    } 
} 

 

The panel is bound to the SelectedProduct property:

<Border x:Name="ProductInfoPanel" 
    Visibility="{Binding SelectedProduct, Converter={StaticResource VisibilityNullConverter}}" 
    Margin="330,50,110,50" 
    CornerRadius="10,10,10,10" 
    BorderThickness="1,1,1,1" 
    Height="260" 
    BorderBrush="#FFB1C69E" 
    Background="#19FFF200"> 

 

When the SelectedProduct is null, the converter will return Visibility.Collapsed and the Border and its content will be hidden. When the SelectedProduct becomes not null, the binding will automatically update the Visibility property of the Border, thanks to the INotifyPropertyChanged interface, that is implemented in the data source.

Posted in: Howto Silverlight

9 Comments

  • Ben Hayat 09 Oct
    Valeri; Excellent Initiative! While you're at this ADO.Net Data Service, could you take a look at the post I made in the SL General section under the title of "Telerik Report and SL"? Thanks!
  • Ben Hayat 09 Oct
    Nice & clever way of using Converter! ;-)
  • Valeri Hristov 10 Oct
    Yesterday I spoke with one of the developers of Telerik Reporting and he told me that what you are looking for is feasible. I think that the Silverlight/Service part should not be much of a hassle once you have the report configured on the server. However, it would take some time to prepare an example, since the Q3 release is quickly approaching and the deadlines are tightening...
  • Ben Hayat 10 Oct
    Valeri, much appreciate your findings about the report. Sounds very promising. As far as times goes, I fully understand your timing and I hope I didn't sound to rush you. Whenever possible, it's ok. However, it's very important for you guys to show this, because you suddenly opening a new door to SL developers, one more reason to join the camp! Thanks Valeri!
  • Ben Hayat 10 Oct
    Just curious, how come this app runs so much faster online (connected to your server) v.s. when I run this app on my machine inside VS. Your online shows the list instant, however in VS after the Green page shows, I see the circle running for a long time (more than five seconds) before I see the data. I'm actually very pleased that it's not the other way around ;-)
  • Valeri Hristov 12 Oct
    The online demo is running on SQL Server and IIS on a very fast multiprocessor machine, while the local application is running on the Web Development server and SQL Express, which are both much slower than their real server counterparts. Also the local application that hosts the web service needs some time to initialize after compilation. I suppose that these are the main reasons for the performance difference.
  • The Q 19 Oct
    A very nice and straight forward sample - although, you did forget to put an order by clause on the Supplier dropdown list. Tsk, tsk! :-)
  • Thank you for the nice words, Q. I will add order by today :)
  • Great combination Silverlight and ADO.Net Data Service and nice after your designer did a bit ;-)

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)