This post demonstrates the different ways of data binding in windows forms. It also discuss the concepts which are key to understanding the things that are new and different in other binding models. We first design the form using Typed Data Set and then modify the form to work with LINQ to SQL and Entity Framework models.
1. Typed Data Set
Create a new windows forms project in Visual Studio and add a typed dataset named AW2008.xsd to project.
Drag the Product, Product Category and Product Subcategory tables to the designer and save the details.
Next we need to use Data Sources window. If it is invisible then display it by selecting Data Menu then select ShowDataSources option from Visual Studio main window.
Now open the Windows Form, expand any tables nodes from data sources window. You will notice, each column node icon represents a default control type assigned to that column and each table icon represents a grid view UI or details view UI.
Generating the UI from Typed Data Set Source
We are creating a Parent\Child UI in which data for each product category is represented in a one-record-at-a-time and subcategories and products are displayed in separate grids.
Now Drag the ProductCategory table to form designer and then change the view mode to Details View as shown in the below
Drag the ProductSubCategory gridview from datasources window under ProductCategory table to form and also drag the Product gridview which is under ProductsSubCategory table to form designer.
This explains the full functional data maintenance form for product categories with navigation and toolbar actions.
More explanation on how this is generated and how the objects property settings are connected.
- The aW2008 in Component Array is an instance of our typed dataset
- BindingSource Component , productCategoryBindingSource which acts as broker between bound controls and database tables.
- Data Adapter classes if you look at the code behind forms, you will notice Fill methods.
- In the Designer generated code formname.designer.cs, you will find the actual data binding code which generated for the controls in the form
1: // productCategoryIDTextBox
4: (new System.Windows.Forms.Binding("Text",
5: "ProductCategoryID", true));
- The form load event contains the following code
1: private void Form2_Load(object sender, EventArgs e)
3: // TODO: This line of code loads data into the 'aW2008.Product' table.
4: //You can move, or remove it, as needed.
6: // TODO: This line of code loads data into the 'aW2008.ProductSubcategory' table.
7: // You can move, or remove it, as needed.
9: // TODO: This line of code loads data into the 'aW2008.ProductCategory' table.
10: // You can move, or remove it, as needed.
2. LINQ to SQL Data Binding to use LINQ to SQL model instead of Typed Data Set , Add new item to the project and name AW2008.dbml
Steps to convert the existing form to bind the data to LINQ to SQL model
- Delete all form component area items except the typed dataset component
- Delete the code from forms load event handler and the last line of code from Bind Navigator Save button’s click event
- Insert the following code immediately before its constructor
- Insert the following code in the form’s load event handler
AW2008DataContext dc = new AW2008DataContext();
productCategoryBindingSource.DataSource = dc.ProductCategories;
- Insert the following line at the end of the Bind Navigator Save button click event handler
Now it works as before when it is working with typed dataset