Presentation is loading. Please wait.

Presentation is loading. Please wait.

Understand Databound Controls 98-362 Windows Development Fundamentals LESSON 4.2A.

Similar presentations


Presentation on theme: "Understand Databound Controls 98-362 Windows Development Fundamentals LESSON 4.2A."— Presentation transcript:

1 Understand Databound Controls 98-362 Windows Development Fundamentals LESSON 4.2A

2 98-362 Windows Development Fundamentals LESSON 4.2A Lesson Overview How are controls bound to a data source? In this lesson, you will learn about:  Basic databinding concepts  Creating bound controls in Microsoft ® Visual Studio ®

3 98-362 Windows Development Fundamentals LESSON 4.2A Anticipatory Set  Create a form to display data from the student information database that you created for Lesson 4.1.  Use Labels and TextBoxes to hold basic information about students, such as name (first and last), age, grade level, and gender.  For now, the TextBoxes will be blank; you will bind them to your database in today’s lesson.  Be sure the form matches the database fields you have created.

4 98-362 Windows Development Fundamentals LESSON 4.2A Data Binding  The process of connecting controls to information in a data source so that it can be displayed or changed by a user.  Allows the user to interact with information pulled from a data source.  Usually, this is a database, but it could be any data source supported by ADO.NET.  Simple binding is used to connect a single data element to a control.  Examples include binding data to Labels, TextBoxes, and TextBlocks.  Complex binding is used to connect multiple data elements to a control.  Examples include binding data to ListBoxes, ComboBoxes, and DataGridViews.  To create databound controls, your project must have data sources, as explained in Lesson 5.1.

5 98-362 Windows Development Fundamentals LESSON 4.2A Simple Binding To implement simple-bound control: 1. In the Designer, select the control you wish to bind. 2. In the Properties window, expand the (DataBindings) property. 3. Select the property you want to bind (usually Text) and click the drop-down arrow for that property. 4. Navigate through the list of Data Sources and select the element that you wish to bind to the control.

6 98-362 Windows Development Fundamentals LESSON 4.2A BindingSource  As you noticed, your Form does not allow the user to move through the rows of data within the database.  Databound controls use a BindingSource to manage their connection to the dataset.  — A BindingSource is an intermediary—or “go-between”– that facilitates communication between bound controls and their corresponding datasets.  A BindingSource provides a wide variety of functionality, such as notifying objects when changes to the data source have occurred.  A BindingSource also provides methods used to navigate through the dataset (such as MoveNext and MoveLast) and adding/deleting records (such as Remove)  In the Designer, you can add a BindingSource by dragging a BindingSource control from the ToolBox.  When you implement simple binding, a BindingSource is created automatically for you.

7 98-362 Windows Development Fundamentals LESSON 4.2A Navigating on a Databound Form  You could easily create event handlers to call the BindingSource methods.  — MoveNext would move you to the next record, for instance.  Visual Studio also provides a control to simplify navigation: BindingNavigator.  The BindingNavigator control provides a user interface for navigating with databound controls.  — The buttons on a BindingNavigator look similar to what you might find on a media player, such as a Zune.

8 98-362 Windows Development Fundamentals LESSON 4.2A Add a BindingNavigator 1. In the Designer, drag a BindingNavigator control from the ToolBox to the form. 2. In the Properties window, set the BindingNavigators BindingSource property to the BindingSource used by your databound controls.  — The BindingNavigator provides buttons on a toolstrip that invoke methods in the BindingSource.  For example, the MoveNextItem button invokes a method called MoveNext that navigates to the next item in the data source. It is disabled if the source is already at the last record or item.  The result is that each control bound to this BindingSource is updated to display the next record when the MoveNextItem button is clicked.

9 98-362 Windows Development Fundamentals LESSON 4.2A DataGridView  A control that displays data in a customizable grid.  A DataGridView is an example of a complex-bound control—it displays multiple data elements at once. 1. From the Designer, drag a DataGridView control from the Toolbox onto your form. 2. The control’s smart tag Window should appear. 3. Choose the appropriate BindingSource from the drop-down list. 4. Browse through the choices to select the individual table you wish to use. 5. Set the other properties as desired.

10 98-362 Windows Development Fundamentals LESSON 4.2A Adding a DataGridView with BindingNavigator You can also add a DataGridView (or other controls) by dragging and dropping from the Data Sources window. 1.Delete any DataGridViews and BindingNavigators on your form. 2.In the Data Sources window, select the table. 3.Select either DataGridView (the default) or Details (which adds a bound control for each row of the table) from the drop-down list. 4. Drag and drop the table onto the form. 5. Run your application.

11 98-362 Windows Development Fundamentals LESSON 4.2A DataGridView at Run Time  Note that changes are not saved to the database file until the user clicks the Update button

12 98-362 Windows Development Fundamentals LESSON 4.2A Lesson Review  Explain how controls are bound to data in Visual Studio.  Write out a step-by-step list of instructions for implementing simple binding to a TextBox.  Summarize the functionality of the DataGridView control.


Download ppt "Understand Databound Controls 98-362 Windows Development Fundamentals LESSON 4.2A."

Similar presentations


Ads by Google