Skip to main content
Email Password   helpLost your password?

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:

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

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

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralErrors when running sample: Columns not found - LastEditDate and CreationDate Pin
Igor Vaschuk
12:39 1 Oct '09  
GeneralMy vote of 2 Pin
Riaan Hanekom
9:38 7 May '09  
GeneralMy vote of 2 Pin
Charith Jayasundara
4:34 25 Nov '08  
QuestionHow do you update the database? Pin
llamont49
8:56 9 Nov '08  
AnswerRe: How do you update the database? Pin
Colin Eberhardt
7:08 12 Nov '08  
GeneralCalendar Control Pin
sashdude
18:16 4 Oct '08  
Generaldoesnt work Pin
jesuscheung
9:45 2 Oct '08  
QuestionHow to add data into DataGrid with out using Data Binding Pin
Harsha Vardhan K
2:22 23 Sep '08  
QuestionItems.Refresh() [modified] Pin
Kenneta
2:18 13 Sep '08  
AnswerRe: Items.Refresh() Pin
Kenneta
2:29 14 Sep '08  
RantYou could at least... Pin
elise.d
1:25 2 Sep '08  
GeneralRe: You could at least... Pin
elise.d
2:45 2 Sep '08  
RantHmm... Pin
Ray Hayes
0:15 14 Aug '08  
GeneralRe: Hmm... Pin
rudigrobler
4:19 14 Aug '08  
General[Message Removed] Pin
ped2ped
6:27 12 Aug '08  
GeneralRe: Good Pin
rudigrobler
21:30 12 Aug '08  


Last Updated 12 Aug 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009