WPF Datagrid and Linq to SQL

This article is written by Pon Saravanan  on 24-Aug-10 Last modified on :08-Feb-11

VB.Net Tutorial - WPF DataGrid

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.

.Net Framework 4.0

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.

Setup WPF ToolKit for .Net Framwork 3.5

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.

DataGrid DataBinding

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.

Fetch Data using Linq to SQL

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

Linq and Lambda to work with data

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.

Screen Capture

Source Code

Markup (*.XAML)

<Window x:Class="MainWindow"
    Title="MainWindow" Height="522" Width="525" >
        <Button Content="Load" Height="23" HorizontalAlignment="Left"
                Margin="416,12,0,0" Name="Button1" VerticalAlignment="Top" Width="75" />
        <DataGrid AutoGenerateColumns="True" Height="169"
                  HorizontalAlignment="Left" Margin="12,41,0,0"
                  Name="DataGrid1" VerticalAlignment="Top" Width="479" />
        <DataGrid AutoGenerateColumns="True" Height="241"
                  HorizontalAlignment="Left" Margin="12,230,0,0" Name="DataGrid2"
                  VerticalAlignment="Top" Width="479" />
        <Label Content="Lambda" Height="28" HorizontalAlignment="Left"
               Margin="12,12,0,0" Name="Label1" VerticalAlignment="Top" />
        <Label Content="Linq" Height="28" HorizontalAlignment="Left"
               Margin="12,206,0,0" Name="Label2" VerticalAlignment="Top" />


Code Behind(*.XAML.VB)

Class MainWindow
    Private Sub Button1_Click(ByVal sender As System.Object,
                              ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim NorthwindProvider As New NorthwindDataDataContext
        Dim CategoryList = NorthwindProvider.Categories.ToList()
        DataGrid1.ItemsSource = CategoryList.Select(
                                    Function(Category) New With

        Dim LinqedCategories = From Category In CategoryList
                               Select New With {
        DataGrid2.ItemsSource = LinqedCategories.ToList()

    End Sub
End Class


- Next »

    AutoGenerateColumns="True" - It is unnecessary because it's default value
    11/24/2010 5:58:37 AM

  • pons
    Please refer the text in .Net Framework 4.0 section, I have explained why.
    2/11/2011 6:02:49 AM

Captcha Image
For you specially:  
Captcha Text Enter the text in the image.(Not Case sensitive)    

Spam Bot Trap


Select Theme