A while back Odi (@kosmatos) tweeted about how easy it was to bind Xceed DataGrid for Silverlight to an OData data source, and he was right. It is easy! So easy in fact that it wouldn't make for much of a blog post. So what I decided to do is show you how to connect to an OData data source as well as demonstrate how to set up the grid to get the most out of it and your data. Of course, you can swap an OData source for another one of your liking.
Obviously, the first step is to connect to an OData data source to get some data. To do so, add a service reference to your Silverlight application, provide an address to an OData source, such as the Netflix catalog, and give it a more appropriate namespace name.
Now that the service reference is added to the project, let's add a property to the MainPage that will expose the DataServiceQuery that contains the Netflix titles, including the total count of all entities in the entity set. This is the property that the grid will bind to to get its data. Note that I have also set the DataContext of the page to itself to make my life easier.
private NetflixCatalog m_catalog = null;
public DataServiceQuery<Title> NetflixTitles
if( m_catalog == null )
m_catalog = new NetflixCatalog( new Uri( "http://odata.netflix.com/Catalog", UriKind.Absolute ) );
So let's get to down to the nitty-gritty and add the datagrid to the page so that we can bind it to the Netflix data. Like most item-containing controls, the grid has an ItemsSource property through which we will bind to the NetflixTitles property. Because I set the data context of the main page to itself and the data context of the grid is the page, a simple "path binding" can be used. Something like this:
And that's it! In theory, there is nothing else that needs to be done. All the data will be loaded in the grid and can be grouped, sorted, filtered, and edited, but it won't be as pretty as it could be.
So yeah, only a couple lines of code and XAML to get the grid to load data from an OData source, but there is more to displaying data than simply having it on the screen. You will most likely want your data to be displayed in an attractive and easy-to-understand manner for your end users. Not a problem! But I'll show you in my next post :)
If you can't wait for the next post, you can always download the full sample and try it out for yourself.