How to use DataGrid in Silverlight

Download Sample Projects (204.88 kb)

 

This article is one of the initial ones in the series that will demonstrate use of DataGrid control in Silverlight to render data from various data sources. My goal here is not to tell you what DataGrid class is or proand cons of DataGrid. We all have used DataGrid in some form or the other in ASP.Net and/or Windows applications. So my intent is to simple outline the steps that you will need to follow to create a simple DataGrid and bind it to a datasource to display the data. In this article I will not go into details about how you can have better control to formatting of results. I will keep it simple. In subsequent articles I will explain more about controlling formatting of each data column or row in the grid.

Getting started

Like any other Silverlight application, you will start with a new Silverlight application project. Simply drag DataGrid control from toolbox into the page. We will make use of AutoGenerateColumns property of DataGrid to let it generate all the columns automatically returned with data source. In next set of articles I will demonstrate how you can control what columns to display and what not to display. So XAML on page looks like as below.


<data:DataGrid x:Name="AdventureWorks_Grid" 
AutoGenerateColumns="True" Height="200" 
Grid.Column="0" Grid.Row="2" 
GridLinesVisibility="Horizontal" />

You can see from snippet above how I have specified few properties to set height of grid and display of horizontal grid lines.

Connect to data

Now we have defined DataGrid control. Only thing left is to connect this grid with data source. You can set ItemsSource or DataContext property of DataGrid to a collection object that implements IEnumerable interface. For the sample project attached with this article I have implemented an ADO.Net DataService that can be used to query data from sample Adventureworks database in SQL2005. Following code how query is being made to ADO.Net data service to get all items in Product table in the database and then returned results are bound to DataGrid.


private void LoadProductsData()
{
DataServiceContext ctx = new DataServiceContext
(new Uri("http://localhost/SilverDataGridWeb/AdventureDaraService.svc"));
ctx.BeginExecute
(new Uri("Product", UriKind.Relative), ProductsLoadCallback, ctx);
}
private void ProductsLoadCallback(IAsyncResult asyncResult)
{
DataServiceContext ctx =
asyncResult.AsyncState as DataServiceContext;
List products = 
ctx.EndExecute(asyncResult).ToList();
AdventureWorks_Grid.ItemsSource = products;
}

Views: 1497

Related

How to use DataGrid in SilverlightHow to use DataGrid in Silverlight? How to customize display of DataGrid in Silverlight?How to use Silverlight control in ASP.Net GridView or DataGridHow to use Silverlight control in ASP.Net GridView or DataGridHow to customize display of columns in DataGrid in silverlight applicationshow to customize display of data columns in data grid in silverlight applicationsHow to find selected row and cell index in Silverlight DataGridHow to find row and cell Index of mouse click inside Silverlight DataGrid?How to find row and column...Adding Buttons To Silverlight DataGridHow to customize Silverlight DataGrid and how to add buttons to each row of Silverlight DataGrid?

Powered by BlogEngine.NET 1.5.1.7
Theme by Naveen Kohli

By Categories