Chapter 10. Using Collections with Windows Form Controls

After completing this chapter, you will be able to

  • Perform simple bindings with the controls.

  • Create two-way bindings with the controls.

  • Understand the sample code.

Simple Binding

Some Windows Form controls can bind to collections that implement IList and IEnumerable by setting the DataSource property of the ListBox, ComboBox, or DataGridView classes to the collection; the following shows an example.

C#

ListBox lb = new ListBox();

lb.DataSource = new int [] { 1,2,3,4,5 };

Visual Basic

Dim lb As New ListBox()

lb.DataSource = New Integer() {1, 2, 3, 4, 5}

To control which property of the items in the collection these controls should display, you set the DisplayMember property of the ComboBox or ListBox ...

Get Developer’s Guide to Collections in Microsoft® .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.