Simple Bound Controls

Team-Fly team-fly    

ADO.NET Programming in Visual Basic .NET
By Steve  Holzner, Bob  Howell

Table of Contents
Chapter 8.   Data Binding in Windows Forms

Now let's switch gears and begin looking at the controls provided by Visual Studio .NET. There are two types of bound controls: simple and complex. Simple bound controls typically bind single properties to one DataSource and DataColumn. The TextBox is an example of a simple bound control. Complex bound controls can bind to a DataSource and multiple DataColumns. In the case of a complex bound control, the DataSource is typically bound to a collection in the control, instead of a single property. An obvious example of a complex bound control is the DataGrid. The simple bound controls are the following:

  • TextBox

  • Label

  • LinkLabel

  • Button

  • CheckBox (CheckAlign, Tag, Text, Checked)

  • RadioButton (CheckAlign, Tag, Text)

  • PictureBox (Tag Property Only, Image Property can be bound through Binding collection)

  • Panel (tag property only)

  • ListView (tag only)

  • TreeView (tag only)

  • Tab Control (tag only)

  • DateTimePicker (Checked, Tag, TextReturns string representation of date; ValueReturns Date data type)

  • MonthCalendar (tag only)

  • ScrollBars (value property)

  • Splitter (tag only)

  • Domain and Numeric UpDown (text and value)

  • TrackBar (value)

  • ProgressBar (value)

  • RichTextBox (text)

  • ToolBar (tag)

  • StatusBar (text, single panel only)

  • Crystal Report Viewer (tag only)

With all these controls, other bindable properties are available through the Binding collection, which can be accessed at design time by clicking the advanced ellipsis button under DataBindings in the Properties window. I would not consider a control that only allows binding to the tag property data bound. Now let's look at some specific controls.

Text Boxes, RichTextBoxes, and Labels

Controls, like text boxes and labels that display text directly, are among the most popular of bound controls, because they can display the text from an individual field of a table, which means you can tailor your application's data display field by field.

You can use the data in any of these controls to update a database (note that a label may not be changed directly by the user , of course, but you can change the contents of a label under programmatic control).

To use these controls, you just work with the DataBinding properties. You can bind the Text property to the DataSource by expanding the DataBindings node in the Properties window and then dropping down this Text property. Select the DataSource and column you wish to bind to. We've already seen examples of binding text boxes to data sources. You can use the first project from this chapter (ADOBook08-01) for these examples (see Figure 8.12).

Figure 8.12. Selecting a data source.


The process is the same for the RichTextBox and the labels.

Check Boxes

You can bind check boxes to Boolean (True/False) fields in databases, and the check box will be checked or unchecked to match the value in that field. (Technically, check boxes actually have three states: checked [True], unchecked [False], and grayed [indeterminate]).

Instead of binding to the Text property, we can bind the CheckBox to the Checked or CheckState properties, as well as CheckAlign and Text. These properties work as follows :

  • Checked Returns the Boolean true if the box is checked or false if the box is cleared. If the box is indeterminate (grayed) it returns false because it is not checked. You should use this property to bind to a Boolean database field.

  • CheckedState Returns an enumeration that corresponds to the actual state of the check box, checked, unchecked, or indeterminate.

  • CheckAlign An enumeration that determines when the check appears relative to the caption (or text). Right or Left are the values.

  • Text The text that appears as the caption of the check box.

Picture Boxes

Picture boxes can no longer bind to images stored in databases as they could with VB 6. There does not seem to be any way to manage binary data in a database with ADO .NET. The only way to do this would be to store the images in disk files and then store a path name in the database. As each row is made current, you can then use the FromFile method of the picture box to get the image into the control. The FromFile method supports URLs as well as standard Windows paths.

DateTimePickers and MonthViews

DateTimePickers let the user select dates and times, and MonthViews present the user with a small calendar. The DateTimePicker supports the following bound properties:

  • Checked Returns True if the date value is checked. The ShowCheckBox property of the control must be set to true.

  • Text Returns a Text representation of the date formatted according to the Format property. Use this to bind the control to a String SQL data type (nvarchar).

  • Date Returns a date data type. Use this property to bind the control to a DateTime SQL data type.

Team-Fly team-fly    

ADO. NET Programming in Visual Basic. NET
ADO.NET Programming in Visual Basic .NET (2nd Edition)
ISBN: 0131018817
EAN: 2147483647
Year: 2005
Pages: 123

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: