DataGrid binding using Linq To SQl WPF


In this article I will discuss how to retrieve the data from a database source and bind to a datagrid in WPF using Linq to SQL. The article is for the beginners who want to learn how to bind the data from database using the Linq To sql (dbml)file as DataContext and ObjectDataProvider in the WPF forms without doing any coding in the code behind file.

Developing the data driven application using the Linq To SQL is same as using Entity framework in the same way that we don’t have to deal with the database directly. That part is done by the .NET framework using the classes which it create while developing the Linq to SQL relationship .

Following is the step by step by step guide to retrieve the data using Linq To SQL. I have created a new WPF project for demo as shown in the figure below. Now I will add a new item Customer.dbml class as shown below Once the Customer.dbml class is added to the VS solution. Double click on it and it will open the designer for the class as shown below After opening the designer, we have to open the server explorer in the visual studio. I am using the Customers table of the northwind database to populate my data grid. Drag and drop the Customers table into the designer of the Customer.dbml. After dropping the Customer table into the designer save the Customer.dbml file. Now open the Customer.cs file for the same Customer.dbml file by right clicking on it and selecting ViewCode. Include the two namespces System.Linq and System.Collection.Generics and create a new functions named GetAllCustomers() as shown in the code below

Linq To SQL DataContext class


using System.Linq;using System.Collections.Generic;namespace DataGridBinding{ partial class CustomerDataContext { public IEnumerable<Customer> GetAllCustomers() { var items = this.Customers.Select(item => item).OrderBy(item => item.ContactName); return items; } }} usingSystem.Linq;usingSystem.Collections.Generic;namespaceDataGridBinding{partialclassCustomerDataContext{publicIEnumerable<Customer>GetAllCustomers(){varitems=this.Customers.Select(item=>item).OrderBy(item=>item.ContactName);returnitems;}}} After writing the above code, now we will write an ObjectDataProvider in the MainWindow.xaml. ObjectDataProvider is used to create an instance of any class in the Xaml file and call the methods from there.

DataBinding using ObjectDataProvider


<Window.Resources> <ObjectDataProvider x:Key="getCustomers" ObjectType="{x:Type local:CustomerDataContext}" MethodName="GetAllCustomers"> </ObjectDataProvider> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding Source={StaticResource getCustomers}}" IsSynchronizedWithCurrentItem="True"></DataGrid> <Grid> </Grid> </Grid> <Window.Resources><ObjectDataProviderx:Key="getCustomers" ObjectType="{x:Type local:CustomerDataContext}" MethodName="GetAllCustomers"></ObjectDataProvider></Window.Resources><Grid><DataGrid ItemsSource="{BindingSource={StaticResourcegetCustomers}}" IsSynchronizedWithCurrentItem="True"></DataGrid><Grid></Grid></Grid>

As soon as we are done with the above coding we can run the application and see that the datagrid is populated with the data from the database as shown in the figure below.

Please find the solution of the article attached here for your reference.

Hope this article has helped you to understand how we can bind the data from a data source using Linq to SQL and ObjectDataProvider in WPF. In my next article I will show how we can update the same data using to the DataBase back.