Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Digging into the DataGrid

From: Silverlight 5 Essential Training

Video: Digging into the DataGrid

The king of the data-bound controls is the Datagrid. This element can show multiple rows and columns, and has sophisticated layout and formatting features. It also supports a rich set of events which fire as the user manipulates the data in the grid. In addition, it enables users to add, edit, delete, select, and sort items from the underlying data source. For this demo, I'm going to use Visual Studio and a project called DiggingIntoDataGrid. Let's start by looking at my business classes.

Digging into the DataGrid

The king of the data-bound controls is the Datagrid. This element can show multiple rows and columns, and has sophisticated layout and formatting features. It also supports a rich set of events which fire as the user manipulates the data in the grid. In addition, it enables users to add, edit, delete, select, and sort items from the underlying data source. For this demo, I'm going to use Visual Studio and a project called DiggingIntoDataGrid. Let's start by looking at my business classes.

I have a Scientist class, which has a few properties: FirstName, LastName, Gender, and a few science-related items. I also have the Scientists class, which derives from ObservableCollection. It is populated with lists of scientist data. I'm going to add these to a grid by going to my BasicGrid demo and showing you the XAML. Here I have a DataGrid. I dragged this from my toolbox onto my designer surface, and then I set the AutoGenerateColumns='True'. Then in my Code Behind--press F7 to see that-- I'm instantiating my Scientists and assigning them to the ItemSource of my grid.

With that one line of code, I can run the application and I instantly get data. Each property in my underlying data source becomes a column in my grid. There's a lot of functionality in this grid already. I can edit the items by clicking here and typing. I can sort them by clicking on the header of the column and even come over here and add new check marks to this checkbox. It does need some work yet. This image is only being shown as a string. I probably need a custom column there or a custom data template.

Speaking of custom columns, that's my next demo. Here, I'm not using the auto-generated columnsl I'm creating each column on my own. So far, I have a single column and that's missing the header. Let's go fix that. Close down the browser, open this CustomColumns.xaml file. I've managed to unpin my tab. The way you put the tab back is to hold down Ctrl key and double-click on the header. On this data grid, I've set the AutoGenerateColumns='False'. I've also set this property RowDetailsVisibilitymode to VisibleWhenSelected.

This will become important in a few minutes. Then in this section, in the DataGrid.Column section, I'm instantiating a new column, and I'm binding it to the LastName property of my data source. Now to add a second column. Copy this data and I'll change this to say FirstName. Now, before I show you the example, let's keep going and looking at more XAML. Down here is something called the RowDetailsTemplate, which matches what we said is up here, RowDetailsVisibilitymode.

So, when you click on a row, this DataTemplate will be shown to the user. Within this data template, I'm creating a Stackpanel, a number of TextBlocks, and an image, and I'm binding them to the underlying source. Let's see what this looks like. I have my two custom columns. I click on one of the rows and I see the Details area. The next thing I might want to do is start changing the headers, the colors, the fonts, and other formatting details.

We'll do that over here in the Formatting Grid area. I'll open FormattingGrid.xaml and again, I'll look at the XAML. It's the same as before; it's a data grid with auto-generated columns. If I'm going to try to format it and make it look beautiful in my designer, I need to see it up here in the designer surface. If you're doing data binding in the Code Behind, you will not see any data here at design time. To solve that, I'm going to add a data source in my XAML and bind to that instead of doing it in my Code Behind.

So, the first thing I want to do is go to my Code Behind, press F7, and comment out this line of code here that's setting the data source. Next, I'll return back to my XAML, and I need to instantiate my Scientists somewhere in my XAML. The place that you do that in Silverlight is inside that Resource section. Resources can be at the DataGrid level or at the Grid level or even at the UserControl level. Here's how you add one. That simple.

Now, I can instantiate types within this boundary. In order to instantiate my type, I need to add my namespace to the XML file. I do that by coming up here and typing in xmlns:local, and then I choose the namespace I want, DiggingIntoDataGrid. Now, I can come down here and use local and I can see all the types that are defined in that namespace. I want to instantiate Scientists. Any item that goes in the Resources section has to have an identifying key.

I have called mine myScience, and now I'm ready to use my source. We'll come down to my DataGrid and set its ItemSource property. And in here, I'm going to use a markup extension, but I'm not going to use a binding markup extension. I'm going to use a resource lookup extension called StaticResource. And as soon as I place that myScience in my StaticResource, you can see the data show up in the design surface.

This is nice because now I can start styling and formatting the data. Let me show you how I might do that. I'm going to go over to the Property grid in Visual Studio and select Columns. Then I'll click on the three dots, this button that has the three dots on it, and then I'll go over here and add a couple of columns. I will start by adding a TextColumn and then I'll add a CheckBoxColumn. Once I've added these two, I'm going to bind them to the underlying data source. Because Visual Studio now knows where the data is coming from in the designer, when I click on DataGridTextColumn and go to the Binding section over here and click in this button area on this side, I'm shown a list of the available sources. FirstName, in my case, is the one I want to choose, and then for the DataGridCheckBoxColumn, I want to blind to the Boolean value WonNobelPrize. Okay.

It's still not right. The header doesn't look right to me. So I'll choose Columns, DataGridTextColumn, and then I'll go to the Header section and type in the string that I want to appear. First, space, Name, Enter, then OK. There's my First Name. For my last demonstration, I'll show you how to group data within a grid. I'm going to open this GroupingData control and show you my user interface.

I have a ComboBox with two items in it: the first one is ScienceArea and the second item is Gender. The user is going to come up and select one of these two items. The ComboBox will fire the SelectionChanged event, and then I'll call my code. Press F7 to look at that code. In the body of this if statement I'm first clearing out any grouping I might already have. That is done by setting the GroupDescriptions property on my data grid. I'm setting it and calling the Clear method. Then I'm getting the value out of the ComboBox, and then finally I'm going back to the GroupDescriptions and I'm adding a new group, data, and I'm using this class here PropertyGroupDescription.

Essentially, this is the name of the field that I want to sort on. Now, when I run the application, go to Grouping, you see that they're currently grouped by ScienceArea. There're two physicists and then two chemists and then a biologist. If I come up here and I choose Gender, I see that something's not right here. I've got male and two females and I see that Linus Pauling has something wrong with his data. This should say M and instead, it says N.

There's another control that excels at editing data. It is called the Data Form, and I'll show it to you in the next movie in this chapter.

Show transcript

This video is part of

Image for Silverlight 5 Essential Training
Silverlight 5 Essential Training

106 video lessons · 5645 viewers

Walt Ritscher

Expand all | Collapse all
  1. 1m 41s
    1. Welcome
    2. Using the exercise files
  2. 35m 0s
    1. Overview of Silverlight
      3m 29s
    2. Setting up a developer computer
      2m 46s
    3. Installing the Silverlight Toolkit
      2m 21s
    4. Exploring the toolkit samples
      2m 35s
    5. Using Visual Studio 2010 to create a Silverlight project
      5m 10s
    6. Using Expression Blend to create a Silverlight project
      3m 13s
    7. Getting to know the Visual Studio interface
      8m 15s
    8. Working in the Expression Blend interface
      7m 11s
  3. 29m 46s
    1. Understanding the Visual Studio project structure
      3m 38s
    2. Creating a Silverlight page
      3m 17s
    3. Compiling your first application
      5m 0s
    4. Using other assemblies in an application
      5m 45s
    5. Deploying a Silverlight application
      3m 43s
    6. Understanding the startup process for an application
      3m 13s
    7. Understanding how users get Silverlight on their computers
      5m 10s
  4. 16m 59s
    1. Exploring the relationship between XAML and .NET
      3m 57s
    2. Using C#
      7m 16s
    3. Exploring the code-behind file
      5m 46s
  5. 43m 22s
    1. Working with XAML elements and Property attributes
      4m 56s
    2. Investigating XAML namespaces
      7m 31s
    3. Converting XAML properties with TypeConverters
      5m 1s
    4. Working with Property elements
      4m 24s
    5. Assigning runtime data with XAML markup extensions
      4m 21s
    6. Digging into the dependency property system
      6m 12s
    7. Creating a custom dependency property
      4m 42s
    8. Understanding attached properties
      6m 15s
  6. 9m 35s
    1. Debugging your code
      5m 17s
    2. Special Silverlight debugging techniques
      4m 18s
  7. 36m 0s
    1. Understanding layout
      4m 55s
    2. Using DockPanel and WrapPanel
      4m 2s
    3. Exploring sophisticated layouts with the Grid
      6m 40s
    4. Absolute positioning with the Canvas panel
      5m 20s
    5. Scrolling content with the ScrollViewer
      3m 28s
    6. Adjusting content alignment, margins, and sizing
      5m 6s
    7. Using the TabControl
      2m 17s
    8. Manipulating elements with transforms
      4m 12s
  8. 20m 32s
    1. Painting the user interface (UI) with SolidColorBrush
      6m 7s
    2. Getting colorful with gradient brushes
      3m 15s
    3. Decorating elements with ImageBrush
      4m 56s
    4. Creating effects with VideoBrush
      6m 14s
  9. 25m 42s
    1. Understanding routed events
      3m 12s
    2. Wiring up event handlers in Silverlight
      6m 4s
    3. Understanding event bubbling
      4m 39s
    4. Exploring mouse events
      7m 43s
    5. Exploring keyboard events
      4m 4s
  10. 32m 31s
    1. Displaying text on the screen
      3m 24s
    2. Gathering text input from the user
      5m 30s
    3. Showing complex text with RichTextBox
      6m 7s
    4. Understanding text overflow and text linking
      3m 14s
    5. Searching content with the AutoComplete type-ahead control
      6m 45s
    6. Using and embedding Silverlight fonts
      7m 31s
  11. 26m 47s
    1. Understanding content controls
      5m 7s
    2. Understanding button controls
      6m 16s
    3. Using the BusyIndicator
      4m 40s
    4. Showing tooltips
      4m 34s
    5. Changing content size with ViewBox
      1m 39s
    6. Exploring more controls
      4m 31s
  12. 55m 36s
    1. Connecting elements with binding
      9m 54s
    2. Using business data in a binding
      9m 21s
    3. Listing data with ItemsControls
      6m 39s
    4. Digging into the DataGrid
      8m 28s
    5. Using the DataForm
      3m 49s
    6. Storing data on the client
      7m 10s
    7. Debugging XAML bindings
      5m 41s
    8. Charting data
      4m 34s
  13. 24m 36s
    1. Creating an Out-of-Browser (OOB) application
      6m 16s
    2. Controlling aspects of an OOB application
      2m 49s
    3. Alerting the user with notification windows
      7m 13s
    4. Hosting HTML content in Silverlight applications
      8m 18s
  14. 30m 2s
    1. Exploring Silverlight trust levels
      3m 51s
    2. Creating an elevated trust application
      5m 57s
    3. Reaping the benefits of XAP signing
      6m 5s
    4. Making cross-domain calls
      5m 1s
    5. Getting started with COM Interop using the Speech API
      4m 21s
    6. Using COM Interop to interact with Microsoft Office
      4m 47s
  15. 16m 33s
    1. Showing child windows
      3m 48s
    2. Using a child window as a dialog window
      5m 7s
    3. Creating native windows
      2m 53s
    4. Maximizing a window with full screen mode
      4m 45s
  16. 13m 20s
    1. Dynamically loading UI content with user controls
      4m 46s
    2. Using the Navigation Framework
      8m 34s
  17. 9m 28s
    1. Using resource files
      3m 53s
    2. Using images in an application
      5m 35s
  18. 16m 32s
    1. Playing audio
      4m 27s
    2. Playing sound effects
      5m 4s
    3. Viewing video content with MediaElement
      4m 58s
    4. Changing video playback speed with TrickPlay
      2m 3s
  19. 14m 40s
    1. Capturing video with a web camera
      4m 16s
    2. Working with a microphone
      3m 47s
    3. Creating print output
      6m 37s
  20. 22m 35s
    1. Understanding Silverlight animations
      4m 35s
    2. Creating animations
      7m 52s
    3. Controlling animations
      4m 18s
    4. Simulating physics with animation easings
      5m 50s
  21. 27m 47s
    1. Placing XAML resources within a FrameworkElement
      6m 47s
    2. Centralizing settings in styles
      4m 8s
    3. Creating an alternate control UI with ControlTemplates
      6m 29s
    4. Dressing up your data with DataTemplates
      4m 33s
    5. Explaining the VisualStateManager: a simple way to manage control states
      5m 50s
  22. 10m 55s
    1. Handling global errors in a Silverlight application
      5m 46s
    2. Showing an application loading screen
      5m 9s
  23. 10m 48s
    1. Integrating with XNA
      7m 40s
    2. Exploring data analysis with PivotViewer
      3m 8s
  24. 1m 6s
    1. Goodbye
      1m 6s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.

Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ .

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.

Mark all as unwatched Cancel


You have completed Silverlight 5 Essential Training.

Return to your organization's learning portal to continue training, or close this page.

Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member ?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferences from the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

Thanks for signing up.

We’ll send you a confirmation email shortly.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.