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

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…

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
| You must Sign In to use this message board. |
|
|
 |
|
 |
I just installed Northwind from MSDN for SQL 2000, and when I run sample I get exception Invalid column name 'LastEditDate'. Invalid column name 'CreationDate'.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Agree with some of the other comments - although this article pointed me to the wpf grid, it hasn't exactly been a learning experience.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
I would like to use this control but after a lot of time spent trying to use it, I've yet to figure out how to save changes in the data base. I'm using it with a simple .mdb (assess database) and can only generate a "not connected error"
Thanks in advance for your help
Lawrence La Mont
|
| Sign In·View Thread·PermaLink | 2.00/5 |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
 |
First thanks for a great WPF component.
I notised when I do ".Items.Refresh();" on the Grid, I get "BindingExpression path error:" for all my fields in my collection and if I have checkboxes with seleced values the "flicker" from tru to false, otherwise the data stays in the Grid.
I have a simple ObservableCollection binded to the Grid.
Is there a way to just update one row instead for all rows that ".Items.Refresh()" do. Or how can I write a value to a sertan cell.
How about "Drag & Drop"? I like to se an example on Drag n' Drop rows within a Grid (move a row), and Drag a row from one Grid to another.
Thanks!
/Ken
modified on Saturday, September 13, 2008 8:11 AM
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
You talk about the CommitingEdit event, for example, but in your sample it's not here... a mistake ? did i look on the right place ?
|
| Sign In·View Thread·PermaLink | 1.00/5 |
|
|
|
 |
 | Hmm...  Ray Hayes | 0:15 14 Aug '08 |
|
 |
Whilst I thank you for pointing me in the direction of the DataGrid, your example application is very minimal. Surely you could have done more than simply bind an IEnumberable<T> to a GridView?
It would have been nice to see an example of binding to a simple collection and manually setting the columns required. You talk about a lot of features that the DataGrid can do, but there are no examples of these!
Regards, Ray
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
|
 |
|
|
 |
|
|