Click here to Skip to main content
12,248,339 members (55,896 online)
Click here to Skip to main content
Add your own
alternative version

Stats

288.3K views
3.8K downloads
54 bookmarked
Posted

Introduction to the WPF DataGrid

, 12 Aug 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
A article show how to use the new DataGrid in WPF

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)

Share

About the Author

rudigrobler
South Africa South Africa
No Biography provided

You may also be interested in...

Comments and Discussions

 
Question[My vote of 1] not great Pin
BillW3321-Jul-15 4:41
professionalBillW3321-Jul-15 4:41 
Questionxfcbv Pin
xjie4-Sep-12 0:52
memberxjie4-Sep-12 0:52 
GeneralMy vote of 2 Pin
SinisaRuzin28-Jun-12 3:57
memberSinisaRuzin28-Jun-12 3:57 
GeneralMy vote of 2 Pin
akjoshi27-Jun-12 2:49
memberakjoshi27-Jun-12 2:49 
GeneralMy vote of 2 Pin
Bohdan Kuts22-Dec-11 3:57
memberBohdan Kuts22-Dec-11 3:57 
GeneralMy vote of 1 Pin
XtErMiNaToR10221-Aug-11 1:58
memberXtErMiNaToR10221-Aug-11 1:58 
GeneralMy vote of 1 Pin
rmbsrichard23-Jun-11 16:00
memberrmbsrichard23-Jun-11 16:00 
GeneralGreat for us beginners, but could be clearer Pin
krashcontrol28-Jan-11 7:35
memberkrashcontrol28-Jan-11 7:35 
GeneralMy vote of 3 Pin
krashcontrol28-Jan-11 7:34
memberkrashcontrol28-Jan-11 7:34 
GeneralMy vote of 2 Pin
StewBob18-Oct-10 10:04
memberStewBob18-Oct-10 10:04 
General"An unhandled exception of type 'System.Security.SecurityException' occurred in Unknown Module." Pin
Mike Gledhill30-May-10 23:55
memberMike Gledhill30-May-10 23:55 
GeneralMy vote of 1 Pin
Alexander Oleshov27-May-10 3:41
memberAlexander Oleshov27-May-10 3:41 
GeneralMy vote of 1 Pin
ivix4u13-May-10 0:11
memberivix4u13-May-10 0:11 
GeneralAnother interesting article about the WPF DataGrid Pin
Christian Moser1-Mar-10 10:03
memberChristian Moser1-Mar-10 10:03 
GeneralMy vote of 1 Pin
Ali BaderEddin22-Feb-10 14:02
memberAli BaderEddin22-Feb-10 14:02 
Generalit sucks. Pin
kelx3922-Feb-10 5:24
memberkelx3922-Feb-10 5:24 
GeneralMy vote of 1 Pin
vikramjb15-Dec-09 2:49
membervikramjb15-Dec-09 2:49 
GeneralErrors when running sample: Columns not found - LastEditDate and CreationDate Pin
Igor Vaschuk1-Oct-09 12:39
memberIgor Vaschuk1-Oct-09 12:39 
GeneralMy vote of 2 Pin
Riaan Hanekom7-May-09 9:38
memberRiaan Hanekom7-May-09 9:38 
GeneralMy vote of 2 Pin
Charith Jayasundara25-Nov-08 4:34
memberCharith Jayasundara25-Nov-08 4:34 
QuestionHow do you update the database? Pin
llamont499-Nov-08 8:56
memberllamont499-Nov-08 8:56 
AnswerRe: How do you update the database? Pin
Colin Eberhardt12-Nov-08 7:08
memberColin Eberhardt12-Nov-08 7:08 
GeneralCalendar Control Pin
sashdude4-Oct-08 18:16
membersashdude4-Oct-08 18:16 
Generaldoesnt work Pin
jesuscheung2-Oct-08 9:45
memberjesuscheung2-Oct-08 9:45 
QuestionHow to add data into DataGrid with out using Data Binding Pin
Harsha Vardhan K23-Sep-08 2:22
memberHarsha Vardhan K23-Sep-08 2:22 
QuestionItems.Refresh() [modified] Pin
Kenneta13-Sep-08 2:18
memberKenneta13-Sep-08 2:18 
AnswerRe: Items.Refresh() Pin
Kenneta14-Sep-08 2:29
memberKenneta14-Sep-08 2:29 
RantYou could at least... Pin
elise.d2-Sep-08 1:25
memberelise.d2-Sep-08 1:25 
GeneralRe: You could at least... Pin
elise.d2-Sep-08 2:45
memberelise.d2-Sep-08 2:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160426.1 | Last Updated 12 Aug 2008
Article Copyright 2008 by rudigrobler
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid