Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Data Binding in Windows Forms 2.0

Windows Forms 2.0 increases support for data binding via the new BindingNavigator and BindingSource objects, which will save you a lot of effort. Find out how you can perform sorting and searching tasks using data binding and simplify the display of master-detail relationships in tables.


advertisement
here's a lot of new ground to cover about data binding in Windows Forms 2.0, so it's best to just jump right in. This article assumes you have SQL Server 2005 Express Edition (download here) installed on your computer. I will also use the pubs sample database familiar to most SQL Server developers. Using Visual Studio 2005, create a new Windows application by choosing New Project from the File menu. Click on the Visual Basic (Windows) project type, and select the Windows Application template. Call the new app something like 'Databinding' and click OK. The first step is to add a new data source to the project. You do so via the Data menu then pick the "Add New Data Source" menu item (see Figure 1).

 
Figure 1. Add Data Source: The figure shows the process of adding a new data source to the project.
 
Figure 2. Data Source Configuration Wizard: Choose your data source from this wizard.
 
Figure 3. Choosing a Database: The figure shows the process of specifying the information of the database server.
The Data Source Configuration Wizard appears (see Figure 2). You have the option to add a database, a Web service, or a business object as the data source. For simplicity, the example in this article will bind directly to a database; however in a real-world scenario, it's better practice to bind directly to a business object so as to achieve a multi-tier architecture. Select Database as the source of data and click Next. In the Data Source Configuration Wizard window, click New Connection…. to establish a connection with the SQL Server. For the server name, enter .\SQLEXPRESS (for locally installed SQL Server 2005 Express) and select the pubs database (see Figure 3). Click Test Connection to verify the connection. Click OK to return to the Data Source Configuration Wizard window and then click Next to proceed to the next step.

In the next step, you will be asked to save the connection string created in the previous step to the application configuration file (Figure 4). You should accept this option as this allows you to change your database server easily by modifying the app.config file after the application has been deployed. Click Next.

 
Figure 4. Saving a Connection String: Here's how to save the connection string into the app.config file.
 
Figure 5. Table Selection: Select the authors table.
In the next step, you will select the table(s) that you want to use from the pubs database. Expand the Tables node and check the authors table (see Figure 5). Click Finish.


To view the data source that you have just added to your project, go to the Data menu and choose "Show Data Sources." You should see the authors table with its various fields listed under the pubsDataset object (see Figure 6).

 
Figure 6. Data Sources: Viewing the data source in the Data Sources window.
 
Figure 7. DataBinding Controls: The four controls at the bottom are added to the form for databinding.
Drag the authors item in the Data Sources window and drop it onto the default Form1 design surface. You will see a DataGridView control added to the form automatically along with a BindingNavigator control and three others (the fourth—AuthorsBindingNavigator—is actually the BindingNavigator control) at the bottom of the form (see Figure 7).



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap