Step 2 of 4: Binding the Grid to a Data Source

In the last step you set up the grid application, but the grid currently contains no data – if you run the application now you'll see a blank grid. In this step you'll continue in Visual Studio by adding an XML data source to your project and binding the grid to the data source.

To add a data source and bind the grid in Visual Studio, complete the following steps:

1.   In the Solution Explorer window, right-click the project and select Add | New Item.

2.   In the Add New Item dialog box, select XML File from the list of installed templates, name the file "Products.xml", and click Add to close the dialog box.

The Products.xml file should now be included in your project, and should have opened automatically.

3.   Replace the existing text in the Products.xml file with the following XML markup and save the file:

      XML to add:

This will add data taken from the Products table of the standard Microsoft Northwind database.

4.   Right-click the project and select Add Reference. In the Add Reference dialog box, locate System.Xml.Linq and click OK to add the reference.

5.   Choose MainPage.xaml, right-click the page, and select View Code in the context menu to open the Code Editor.

6.   At the top of the Code Editor, add the following code to import namespaces:

      Visual Basic

      C#

7.   Replace the existing code with the following code to initialize the data source, and bind the C1DataGrid.ItemsSource property to the XML data source:

      Visual Basic

      C#

 What You've Accomplished

If you save and run your application you'll observe that the grid is now populated with data from the Products.xml file:

 

 

You've successfully bound DataGrid for Silverlight's C1DataGrid control to an XML data source.  In the next step you'll customize the appearance and behavior of the C1DataGrid control.


Send us comments about this topic.
Copyright © GrapeCity, inc. All rights reserved.