Google
Web Netomatix

How to use DataGrid in Silverlight?

by Admin 5. February 2010 07:49
DataGrid is one of the most important control when it comes to displaying tabular kind of data. We all implement rendering of such data in some of table format. Developers who has been using ASP.Net are very familiar with controls like DataGrid and GridView. Good news is that there is equivalent DataGrid control for Silverlight as well. And for most part it is used the same way as you do in ASP.Net but with different syntax and different way of customizing display of it. In this post I am going to show a very plain and simple use of DataGrid control in Silverlight and then in subsequent posts I will build on top of this post to show some more advanced uses of DataGrid control.

Silverlight Tool Kit

You may already know this and already has it on your development machine, but I will mention it for sake of completness. You are going to need Silverlight Toolkit to use DataGrid. Yes, the control is not part of core Silverlight. Microsoft has developed it as part of the tool kit that has lot of usefull controls. You can read more about this tool kit and controls on the site. For now, down the toolkit and install it on your machine.

Reference assemblies

In your Silverlight project, you will need to add reference to System.Windows.Controls.Data assembly. This is the assembly where DataGrid control is defined. Now you can include DataGrid control on your XAML file. If you tried to add mark up like below on your page, you are going to get error telling you that DataGrid is not recoganized.


<DataGrid x:Name="MyGrid" />

You have to treat DataGrid control like a UserControl in ASP.Net where you have to specify a tag prefix and assembly on top of your page to indicate use of that control. In Silverllight you do this my including xml namespace tag for that silverlight control. In my case I have the following xmlns entry on my XAML file.


xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"

This tells the application that I am going to use prefix data for the controls that are present in assembly System.Windows.Controls.Data. Based on this you can now add the following like of mark up on XAML file to include DataGrid on your page.


<data:DataGrid 
  x:Name="CommentsGrid" 
  Height="300" 
  AutoGenerateColumns="True" 
  IsReadOnly="True">
</data:Grid>

Attach To Data Source

Now that we have included DataGrid control on the page, we need to attach it to some data source to show some results. Very much like ASP.Net, you will attach an enumeration to this control. The difference is that here it is done through property name ItemSource. Following line of code shows how it is done for silverlight DataGrid control.


List _unmoderatedComments = new List();

void BindGrid()
{
 CommentsGrid.ItemsSource = _unmoderatedComments;
}

As you can see that DataGrid is bound to a List of Comment objects. So far so good, very much like your ASP.Net DataGrid or GridView.

Columns to Display

In the mark of DataGrid above, I have explcitly set AutoGenerateColumn property to true. This actually is default value. What this means is that when DataGrid is bound to the collection, it will generate column for each data field or property for the objects in the collection and display them. Thsi is same behavior you see in ASP.Net data grid.

Explcitly specifying Columns

When you are dealing with some real application most of the time you control the columns you want to display and how you want to display them. Silverlight DataGrid does allow you to do so. Fisrt, you will set AutoGenerateColumns property to false. Following mark up shows how you will specify the columns that you want to display.


<data:DataGrid x:Name="CommentsGrid" Height="300" 
  AutoGenerateColumns="False" IsReadOnly="True">
 <data:DataGrid.CellStyle>
  <Style TargetType="data:DataGridCell">
  <Setter Property="VerticalAlignment" Value="Top"></Setter>
  </Style>
 </data:DataGrid.CellStyle>
 <data:DataGrid.Columns>
  <data:DataGridTextColumn Header="Comment Date" 
    Binding="{Binding CommentDate}"></data:DataGridTextColumn>
  <data:DataGridTextColumn Header="Comment Text" Binding="{Binding Text}">
  <data:DataGridTextColumn.ElementStyle>
  <Style TargetType="TextBlock">
   <Setter Property="TextWrapping" Value="Wrap"/>
  </Style>
  </data:DataGridTextColumn.ElementStyle>
 </data:DataGridTextColumn>
 <data:DataGridTemplateColumn>
  <data:DataGridTemplateColumn.CellTemplate>
   <DataTemplate>
    <StackPanel Orientation="Horizontal">
     <Button x:Name="ApprovedButton" Content="Approved" 
       Click="ApprovedButton_Click" Height="30" Margin="3"></Button>
     <Button x:Name="DeleteButton" Content="Delete" 
        Click="DeleteButton_Click" Height="30" Margin="3"></Button>
     <Button x:Name="SpamButton" Content="Mark Spam" 
       Click="SpamButton_Click" Height="30" Margin="3"></Button>
   </StackPanel>
   </DataTemplate>
  </data:DataGridTemplateColumn.CellTemplate>
 </data:DataGridTemplateColumn>
 </data:DataGrid.Columns>
</data:DataGrid>

You will need to add Columns section under your DataGrid control definition and then specify each column that you would want to display. And to bind the column to particular property or field, you will use Binding property. For this post I am going to keep the dicussion to simple binding of the column to property of the object. I will discuss more advanced use in subsequent posts. You can see it is very similar to how you are used to doing things in ASP.Net.

And to accomodate more customized view of the column, you will use DataGridTemplateColumn where you can layout the template of the view of that column. This is also similar to template column in ASP.Net.

More...

For this post I am going to leave this discussion to this simple display of data. I will be discussing more about use of Silverlight DataGrid in subsequent posts. This post should get you started with use of it now.

Views: 3390

Tags: ,

DataGrid | Silverlight

Comments

2/6/2010 7:25:46 AM #

ucvhost

How to use Remote Desktop Connection for the two systems which have same IP Address?

ucvhost United States

2/11/2010 12:16:31 AM #

Compare Credit Cards

I appreciate you for posting this tutorial here. Thanks.

Compare Credit Cards United States

2/11/2010 12:17:47 AM #

Non Chexsystems Banks

I am glad your shared this in much detail enough to understand it. Thank you.

Non Chexsystems Banks United States

2/11/2010 12:34:42 AM #

Non Chexsystems Banks

Thanks for showing us how to use Datagrids in Silverlight. I have been looking for sources to know how & here it is.

Non Chexsystems Banks United States

2/11/2010 12:35:06 AM #

New Bank Account

Thanks for showing us how to use Datagrids in Silverlight. I have been looking for sources to know how & here it is.

New Bank Account United States

2/11/2010 12:37:03 AM #

Second Chance Checking

This was a great post on Silverlight. It helped me lots. Thanks.

Second Chance Checking United States

2/12/2010 3:15:10 AM #

ucvhost

Hi. I wanted to drop you a quick note to express my thanks. I've been following your blog for a month or so and have picked up a ton of good information as well as enjoyed the way you've structured your site.

ucvhost United States

2/16/2010 7:38:28 AM #

usa mapquest driving directions

I will certainly bookmark it or even subscribe to your rss feeds just to be updated on your new posts

usa mapquest driving directions United States

4/6/2010 1:16:28 AM #

giochi del casinò in rete

I will recommend my friends to read this.I will bookmark your blog and have my children check up here often.I am quite sure they will learn lots of new stuff here than anybody else!....There is also a lot of visual symbolism that is sometimes subtle, sometimes not so subtle..

giochi del casinò in rete India

4/15/2010 6:21:58 AM #

annuity qoutes

This is a good tutorial. Thanks for sharing it. I need this with the programming thing.

annuity qoutes United States

4/18/2010 8:37:07 PM #

Virginia Beach Web Design

you will set AutoGenerateColumns property to false

This is where I kept messing up. Such a simple mistake and it caused me hours of grief

Virginia Beach Web Design United States

5/3/2010 3:04:49 PM #

auto insurance quotes

Thanks for showing us how to use Datagrids in Silverlight. I have been looking for sources to know how & here it is.

auto insurance quotes United States

5/12/2010 6:00:03 AM #

online payday loans

First of all thanks a lot for the useful and informative article. I have to do a little research in my university about the use of DataGrid in Silverlight  so I am so glad that I have found this your post. The information which you have shared for us is really good explained and I will definitely will use it in my work. Thanks a lot one more time for the useful and detailed post and I will be waiting for more such great articles like this one from you in the nearest future.

online payday loans United States

5/25/2010 12:43:30 AM #

wholesale laptop adapter

that there is equivalent DataGrid control for Silverlight as well. And for most part it is used the same way as you do in ASP.Ne

wholesale laptop adapter Oman

5/25/2010 3:37:34 AM #

Network Security Miami

Nice to be visiting your blog, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.

Network Security Miami United States

5/25/2010 3:38:01 AM #

Email Marketing

Your blog is so informative … keep up the good work!!!!

Email Marketing United States

5/28/2010 5:22:38 AM #

Call to India

Really appreciate this post. It’s hard to sort the good from the bad sometimes, but I think you’ve nailed it!

Call to India United States

5/30/2010 3:49:07 AM #

Shimla Hotels

Thanks for the useful info!

Shimla Hotels United States

6/4/2010 5:53:56 PM #

safety equipment



Wish I have the luxury of time to consider using the benefits these site can offer.....

safety equipment United States

6/10/2010 3:09:27 AM #

Phone Systems for Small Office

This article shows you how to work with a DataGrid control available in Silverlight 2.0. Article also demonstrates some formatting and data

Phone Systems for Small Office United States

6/11/2010 6:24:40 AM #

Internet Advertising Agency

This is very useful article for me, Thanks for posting

I expect more from you
Thanks in advance

Internet Advertising Agency United States

6/13/2010 9:53:16 PM #

world cup

This is really nice blog, I am very impressed.

world cup Vietnam

6/22/2010 1:52:43 AM #

Tennessee Titans Tickets

Useful information ..I am very happy to read this article..thanks for giving us this useful information. Fantastic walk-through. I appreciate this post.

Tennessee Titans Tickets United States

6/22/2010 1:53:33 AM #

Xbox

This is my first visit here. I found some really interesting stuff in your blog especially this discussion. Keep up the good work.

Xbox United States

6/22/2010 1:54:03 AM #

Juega Poker

I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and learned a lot from them. You are doing some great work. Thank you for making such a nice website.

Juega Poker United States

6/25/2010 12:52:10 AM #

coach purses


I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post. Contact some state or county agencies in your state to get in contact with some facilities that may be places of interest to you.

coach purses United States