Monday, March 19, 2012

WPF Datagrid and Linq to SQL

When I started working in WPF, in my first sample I tried to locate the control in the toolbox. To my surprise I couldn’t find it. I started searching it in the web for some information on that. I found though the DataGrid is not available in WPF under the framework. It is available from CodePlex for public downloading.

The above paragraph is not applicable for the .Net framework 4.0, as the .net framework 4.0 already preloaded with the WPF DataGrid control. Only difference I faced is, when you are dragging the control from the toolbox. The CodePlex DataGrid is putting defaults to the AutoGenerateColumns as true but the .Net framework control defaults the AutoGenerateColumns as false.

After downloaded from CodePlex, install it. It will not be available in the tool box right out of the box. You have to choose the installed component to be listed in the toolbox. In order to get DataGrid on Toolbox we should select from the list of components. This is available in the “Choose Tool Box Item” dialog box and under the WPF Components section.

Now no matter we are using .net Framework 4.0 or .Net Framework 3.5 we have a DataGrid to start work on. There are not many changes from the way we bind the ListBox in WPF. But here we got a lot of options as we have in GridView. For e.g., we have some standard set of predefined columns for simple usages. For advanced usages we can go for the template controls as we do in GridView.
For making this sample simpler, we go for AutoGenerateColumns = True. This has taken care of all the column creations. So we can just bind the WPF DataGrid with ItemsSource.

It is much easier to use Linq to SQL to fetch the data from the database.  Firstly add the Linq to SQL class by choosing them from Add New Item window as shown in the following screen shot
Go to Add New item \ Select Linq to SQL Classes
Name it as NorthwindData.dbml

Then drag the table category from the server explorer window to the NorthwindData.dbml’s designer

Now you can simply bind the data as follows
DataGrid1.ItemsSource = (New NorthwindDataDataContext).Categories

Now it is time to move a bit to know how to use the Linq and Lambda to deal with data. Using the lambda expressions and select method of the list we are trying to transform the category object to anonymous type. Basically we don’t want to display code and picture in the DataGrid. So we don’t pass it to the control.
Though it is easier to do it with templates, I am trying to explain how to transform objects easily using the lambda expressions.  Using lambda expression you can create a new anonymous type using the New With keyword. After specifying the new with whatever you are putting inside {} will become members of the anonymous type.
Using linq will be much more readable than the Lambda expressions.

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="522" Width="525" >

No comments:

Post a Comment