Binding DataGridView to DLinq Object
DLinq (.net Language Integrated Query for Relational Data) is an O/R mapping infrastructure that will help manipulating relational data as objects. It also does not compramise on the ability to query. I tried to dirty my hands playing a bit with it and use it for databinding winforms controls and posting my findings here.
I choose the well known datagridview control introduced in VS 2005. Here is how you use the DGV to get data from the Customers table of the Northwind DB in VS2005. I am using C# language.
1. Open a WinForms App in C#
2. Add a DGV to the form
3. On the Smart tag of the DGV, Click "Add Data Source"
4. Add DataSource Wizard comes up. Select "Database", Connect to the Server and select NorthWind DB, then select Customers Table.
5. Build the App. Run the App. On the running form you see contents of Customers Table in the DGV.
With DLinq, the approach is pretty similar but we use "Object" DataSource instead of selecting "Database" in the Datasource wizard.
Here are the steps
1. Create a new Winforms App in C#.
2. Add new item "Linq to SQL Classes" item to the project. Note that it creates "DataClasses.dbml" file. The codebehind file for this is "DataClasses.Designer.cs" file.
3. In the Server Explorer, create a connection to and connect to the Northwind Database.
4. Drop the Customers Table from the Server Explorer's Northwind Database onto the designer of the "dbml" file. (You can see now the designer contains the Customer Object)
5. Build the app (Very important, otherwise you will not see the Customer Obj in the next steps)
6. From the Data Menu, Select Add New Data Source
7. Select Object for choose Data Type and select Customer Object of WindowsApplication as DataSource.
8. Drop the Customer node from the DataSource Window to the form. CustomerBindingSource1 gets added to the form.
Your/My Expectation : Running the app will show the contents of the Customer table on the form.
Result : Running the app does not show the Customer table data on the form.
We have to bind to the DataClasses to get the data.
9. Modify the form1.cs to look similar to this
public partial class Form1 : Form
{
//newly added : start
DataClasses1DataContext datacontext1;
//newly added : end
public Form1()
{
InitializeComponent();
//newly added : start
datacontext1 = new DataClasses1DataContext();
customerBindingSource.DataSource = datacontext1.Customers;
//newly added : end
}
}
10. Now build your solution and run the solution.
You can see the form has the contents of the Customer table when Run.
Comments
Anonymous
July 09, 2007
Hi nagasr, Thanks for your post. That helped me get started with Linq. Editing cells in the DGV I think you also need datacontext1.SubmitChanges() on maybe the "CellEndEdit" event. FrankAnonymous
October 31, 2007
Hi, Great post, just what I was looking for to get me started with WinForms DataBinding with LINQ! BrendanAnonymous
January 18, 2008
Apparently the above does not work with VS 2008 using SQL Compact. Just FYI.Anonymous
May 27, 2008
Another method of doing this is to bind to the list returned by the GetNewBindingList() method of any System.Data.Linq.Table<TEntity> object. This method creates a new list for binding to a data source.Anonymous
July 20, 2008
How do you handle inserts? For some reason the datagridview does not reflect the changes we make programmatically until the application restarts.