Complex Bound ControlsListBoxes, ComboBoxes, and the DataGrid

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

The complex bound controls allow the control to be bound to more than one column at a time. The DataGrid displays multiple rows of data from a DataTable. The ListBox and ComboBox can display a single column but multiple rows from that column.

ListBoxes and ComboBoxes

The ListBox and ComboBox controls automatically populate their lists from a DataSource. They can also bind their Text property to a column in a DataTable. A combo box is a combination of a list box and a text box. The text box can be bound to one column, while the list box can get its list from another table. Likewise, the list box gets its list from a DataTable. It can also bind its Text property (which is the value of the current row of the list) to another DataTable.

Binding the List

The DataSource, DisplayMember, and ValueMember properties affect the list portion of the combo box. These apply to both the combo box and the list box:

  • DataSource The DataSet and DataTable or DataView to draw data from.

  • DisplayMember The value to display in the text portion of the combo box and the list. The typical use for this is to display a description for an underlying code value.

  • ValueMember The underlying code value that will be stored in the database.

Binding the Data

The Binding collection properties that can be bound though the Properties window are for binding the selections in the combo box or list box to the database.

  • SelectedItem Returns the ListItem object of the selected item.

  • SelectedValue Returns the ValueMember value of the selected item.

  • Text The string displayed in the text box portion of the control. NOTE: This does not necessarily have to correspond with any item in the list, if the DropDownStyle of the control is a dropdown list. If it is dropdown list then the Text property will be one of the DisplayMember values in the current list. Applies to the ComboBox only.

For the ListBox, the Text property refers to the displayed text in the selected item in the list even though it is not listed in the Properties window. This is the same behavior as the VB 6 list box. It will be the same as the SelectedItem value for the ListBox so it is not listed as a separate bindable property.

You can search the ComboBox control by entering text into the text box of the control. That value is located in the list, and the current list item is set to that item if it is found. If the item is not found, the SelectedValue property is set to null. The ListBox control also has an automated search mode.

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: