5,695,118 members and growing! (15,261 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls     Beginner License: The Code Project Open License (CPOL)

Introduction to the WPF DataGrid

By rudigrobler

A article show how to use the new DataGrid in WPF
C#, Windows, .NET (.NET 3.5, .NET), WPF, Dev

Posted: 12 Aug 2008
Updated: 12 Aug 2008
Views: 20,948
Bookmarked: 19 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
20 votes for this Article.
Popularity: 3.87 Rating: 2.97 out of 5
4 votes, 20.0%
1
6 votes, 30.0%
2
3 votes, 15.0%
3
1 vote, 5.0%
4
6 votes, 30.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Introduction

After WPF was released without a DataGrid, Microsoft received loads of criticism! Thy have finally released the highly anticipated DataGrid (CTP)! Here is a short introduction on how to use the DataGrid

Required

Download the WPFToolkit here

To run my sample application, also install the Northwind database

Getting started

Create a blank WPF application and add a new item (LINQ-to-SQL Class). Drag the required tables from the Northwind database onto the design surface

LINQ2SQL.jpg

Now that we have a simple database to work from, lets add the DataGrid to our project. Add a refrence to WPFToolkit.dll and add the namespace to your main window's XAML

xmlns:dg="clr-namespace:Microsoft.Windows.Controls;assembly=WpfToolkit"

The simples DataGrid we can create is by adding the DataGrid in our window and setting the AutoGenerateColumns to True! This will automatically generate a column for each field in the table. I also gave the DataGrid a name so I can access it in my code-behind

<dg:DataGrid 
    x:Name="NorthwindDataGrid"
    AutoGenerateColumns="True" />

All that is now left to do is get the table from the database (using my created LINQ-to-SQL class) and setting the DataGrid's ItemSource (The DataGrid inherits from ItemsControl)

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    using (NorthwindDataContext dc = new NorthwindDataContext())
    {
        NorthwindDataGrid.ItemsSource = dc.Customers.ToList();
    }
}

And here is the DataGrid in its full glory…

DataGrid.jpg

That was easy, what else can this baby do?

By default, the DataGrid supports resizing, reordering, sorting, adding and removing. This behavior can be controlled with the following properties:

  • CanUserAddRows
  • CanUserDeleteRows
  • CanUserResizeColumns
  • CanUserReorderColumns
  • CanUserSortColumns

On editing a cell, the following events gets fired...

  • PreparingCellForEdit
  • BeginningEdit
  • CommitingEdit
  • CancelingEdit

The DataGrid derives from MultiSelector allowing true multi row selection scenarios. The selection mode is controlled by setting SelectionMode to Single or Extended

The SelectionUnit can also be changed to only select a cell (DataGridSelectionUnit.Cell), row (DataGridSelectionUnit.FullRow) or cell/row (DataGridSelectionUnit.CellOrRowHeader)

The DataGrid also fully support clipboard copy/past

More Resources

  • Read more about the full feature list of the DataGrid here
  • Vincent Sibal also has some excellent content on his blog about the subject

As always, please vote for the article (And if you thought it sucked, please leave a comment telling me how to make it better in the future)

Rudi Grobler

http://dotnet.org.za/rudi

History

12-Aug-2008 - Initial version released

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

rudigrobler



Location: South Africa South Africa

Other popular Windows Presentation Foundation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 15 of 15 (Total in Forum: 15) (Refresh)FirstPrevNext
GeneralMy vote of 2memberCharith Jayasundara4:34 25 Nov '08  
GeneralquestionsmemberTibor Blazko23:57 13 Nov '08  
QuestionHow do you update the database?memberllamont498:56 9 Nov '08  
AnswerRe: How do you update the database?memberColin Eberhardt7:08 12 Nov '08  
GeneralCalendar Controlmembersashdude18:16 4 Oct '08  
Generaldoesnt workmemberjesuscheung9:45 2 Oct '08  
QuestionHow to add data into DataGrid with out using Data BindingmemberHarsha Vardhan K2:22 23 Sep '08  
QuestionItems.Refresh() [modified]memberKenneta2:18 13 Sep '08  
AnswerRe: Items.Refresh()memberKenneta2:29 14 Sep '08  
RantYou could at least...memberelise.d1:25 2 Sep '08  
GeneralRe: You could at least...memberelise.d2:45 2 Sep '08  
RantHmm...memberRay Hayes0:15 14 Aug '08  
GeneralRe: Hmm...memberrudigrobler4:19 14 Aug '08  
General[Message Removed]memberped2ped6:27 12 Aug '08  
GeneralRe: Goodmemberrudigrobler21:30 12 Aug '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 12 Aug 2008
Editor:
Copyright 2008 by rudigrobler
Everything else Copyright © CodeProject, 1999-2008
Web18 | Advertise on the Code Project