Site Search

Lab 9: Binding to Data Controls

Learning Objectives for Lab 9

After completion of today's lab, you should be able to

Databases

A lot of different applications, whether Windows, Web, or console, use a relational database to store the program's data. Inside the program, queries written in Structured Query Language (SQL) are used to retrieve and manipulate the data.

.NET provides a number of classes to simplify the process of reading and writing to databases. Using these classes produces code that is easily ported from one database to another (for example, develop with Access and move to SqlServer)

We will use MS Access to create a very small database to use with Lab 9

Activity: Using a MS Access Database with Visual Studio 2008

Courses Table Screen Snapshot

Activity: - Create a MS Access Database to use with Lab9

  1. Use the MS Access program to create a new blank database named Lab9 stored in your u:\ drive
  2. Choose Create a table in design mode view and add a table to the database named Courses
  3. In the designer view, add four columns to the new table of the type shown at left
  4. Make the CourseId the primary key and set its Data Type to AutoNumber.
  5. Save the new table as Courses

Activity: Adding Data to the Table using Access

Courses Data

We can use Access to add or edit data in the database tables directly. Let us add some courses to our Course table

Activity: - Add records to Courses

  1. Open the Courses table and add the data for five courses, letting Access autonumber the CourseId column and using the tab key to advance from one column to the next.

Working with DataSets

Datasets are in-memory representations of our database structure and data. We can fill datasets with database data, work with the data in our application, and then save the data back to the database when it changes

Courses.xsd

Activity: - Create new WindowsUI project in solution U:\Lab9

  1. Create a Visual C# Windows Application named WindowsUI in solution U:\Lab9.
  2. Change the name of the Form1 file to MainForm.cs and the text displayed in MainForm's title bar to Lab 9

Activity: - Add a DataSet for your Courses table

  1. Use the Data menu to Add a New Data Source to your WindowsUI project
  2. Select the Lab9 access database
  3. Click no if prompted to add the database to your project
  4. Check the Courses table as your Database object and accept the default Lab9DataSet name
  5. Click Finish
  6. You should see a Lab9DataSet.xsd file added in Solution Explorer

Displaying Data on a Form

Data Sources Window

Activity: - Displaying the Data in a DataGridView

  1. View the MainForm in Design View
  2. From the Data menu, select Show Data Sources
  3. It should open a new Data Sources window with your Courses DataSet visible
  4. Select DataGridView form the Courses drop down list
  5. Drag your Courses object from the Data Sources window onto your MainForm
  6. You should see big changes on your MainForm and in its designer tray
  7. Compile and run your application

In addition to the DataGridView, we also have available a Details view

Activity: - Displaying the Data in a Details View

  1. Add a new windows Form to your project named DetailsViewForm
  2. From the Data Sources window, select the Details view from the Courses drop down list
  3. Drag your Courses object from the Data Sources window onto your DetailsViewForm
  4. Add a button on your MainForm to instantiate and show your DetailsViewForm
  5. Compile and run your application

Data Binding to Controls

The built-in DataGridView and Details view controls are useful, but more often we need to design our own interface

Many of the .net controls allow binding with a data source

Activity: - Data binding to controls

  1. Add a new windows Form to your project named CourseViewerForm
  2. Draw over to your form a ListBox and four TextBoxes
  3. From the top of the Tool Box, drag over a Lab 9 DataSet and a CoursesTableAdapter
  4. Change the action of the MainForm button to instantiate and show your CourseViewerForm
  5. Compile and run your application

Surprise, nothing happens by magic. Add a Load event handler and fill the DataSet with the following code.

      private void CourseViewerForm_Load(object sender, EventArgs e)
      {
         this.coursesTableAdapter1.Fill(this.lab9DataSet1.Courses);
      }
Course Browser Form

Activity: - Binding your ListBox to the coursesBindingSource

  1. Use the smart tag on your ListBox and check use data bound items
  2. For Data Source, select Courses under Project Data Sources, Lab9DataSet
  3. For Display Member, choose Department
  4. For Value Member, choose CourseID
  5. Compile and run your application

Activity: - Binding your TextBoxes to the coursesBindingSource

  1. In the CourseViewerForm Design View, select the first TextBox and use the Properties window to set the DataBinding Text property to coursesBindingSource - CourseID
  2. Do the same for the other TextBoxes
  3. Compile and run your application

Activity: - Saving the data

  1. Add a button to CourseViewerForm to save the changed data
  2. Add a button click event handler
  3. Copy and paste the following code to save the data
  4. Compile and run your application
      private void saveButton_Click(object sender, EventArgs e)
      {
         this.Validate();
         this.coursesBindingSource.EndEdit();
         this.coursesTableAdapter1.Update(this.lab9DataSet1.Courses);
      }