Microsoft KB Archive/316302

= How to bind a DataGrid control to an ArrayList of objects or structures by using Visual Basic 2005 or Visual Basic .NET =

Article ID: 316302

Article Last Modified on 12/6/2006

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q316302





For a Microsoft Visual C# .NET version of this article, see 316303.



SUMMARY
This step-by-step article describes how to bind an ArrayList of objects to a DataGrid control. The example consists of a Microsoft Windows Form with a DataGrid control to display object property values, and four command buttons to browse the rows of the DataGrid control.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that are required:
 * Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET

This article assumes that you are familiar with the following topics:
 * Visual Basic programming concepts

Design the class
A class that is to be bound to a control must have property accessors. Any property that is to be bound must have Property Set and Property Get methods. The sample class that is used in this article has three members (only one is shown here.) A parameterized constructor has also been provided, but is not a requirement. Public Class guitar Private m_make As String Private m_model As String Private m_year As Short

Public Sub New(ByVal make, ByRef model, ByVal year) m_make = make m_model = model m_year = year End Sub Public Property make As String Get Return m_make End Get Set(ByVal Value As String) m_make = Value End Set End Property End Class

Add class instances to an ArrayList
To create instances and add them to the ArrayList, follow these steps:
 * 1) Declare an ArrayList.
 * 2) Create instances of the class, and then add the instances to the ArrayList.

Private al as New Arraylist

al.Add(New guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958)) al.Add(New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964)) al.Add(New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971))

Bind the ArrayList to the DataGrid
After the ArrayList has been populated, set the DataSource property of the DataGrid control to the ArrayList. The columns in the DataGrid control are populated based on the properties for which in-scope property accessors exist. DataGrid1.DataSource = al

Provide a means to browse the ArrayList
You can use CurrencyManager to browse through the ArrayList. To do this, associate CurrencyManager with the BindingContext of the control (in this case, the ArrayList). Private cManager As CurrencyManager

cManager = CType(DataGrid1.BindingContext(al), CurrencyManager) The CurrencyManager class has a Position property that you can manipulate to iterate over the members of the ArrayList. By adding to, or subtracting from, the current value of Position, you can browse the rows of the DataGrid control. 'Move forward one element. cManager.Position += 1 'Move back one element. cManager.Position -= 1 'Move to the beginning. cManager.Position = 0 'Move to the end. cManager.Position = al.Count - 1

Step-by-step example
 In Visual Basic2005 or in Visual Basic .NET, create a new Windows Application project. Form1 is created by default. On the Project menu, click Add Class to add a class to the project.  Replace the code in Class1.vb with the following: Public Class guitar Private m_make As String Private m_model As String Private m_year As Short

Public Sub New(ByVal make, ByRef model, ByVal year) m_make = make m_model = model m_year = year End Sub Public Property make As String Get Return m_make End Get Set(ByVal Value As String) m_make = Value End Set End Property Public Property model As String Get Return m_model End Get Set(ByVal Value As String) m_model = Value End Set End Property Public Property year As Short Get Return m_year End Get Set(ByVal Value As Short) m_year = Value End Set End Property End Class  Close the Class1.vb code window, and then switch to the Form Designer. Add a DataGrid control to Form1. Size the DataGrid control to accommodate four columns and three rows. Add four Button controls to Form1, and then arrange the buttons horizontally.</li> Change the Text property of Button1 to Next.</li> Change the Text property of Button2 to Previous.</li> Change the Text property of Button3 to First.</li> Change the Text property of Button4 to Last.</li>  Add the following code to the Form1 class: Private cManager As CurrencyManager Private al as New ArrayList </li>  Paste the following code in the Form1_Load event: al.Add(New guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958)) al.Add(New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964)) al.Add(New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971)) cManager = CType(DataGrid1.BindingContext(al), CurrencyManager) DataGrid1.DataSource = al                   </li>  Paste the following code after the Form_Load procedure: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click cManager.Position += 1 End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click cManager.Position -= 1 End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click cManager.Position = 0 End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click cManager.Position = al.Count - 1 End Sub </li> Build and run the project.</li> Click the command buttons to move among the rows of the DataGrid control. Note that you can edit the values of the objects if desired.</li></ol>

Using a structure instead of a class
The rules for binding a structure are the same as the rules for binding an object. Property (that is, member) accessors are required. A structure that is created for this purpose resembles a class.

To bind to an ArrayList of structures, follow these steps: <ol>  Change the definition of the Class1.vb class module in the example from Public Class guitar '... End Class to the following: Public Structure guitar '... End Structure </li> Build and run the example program again, and verify that it functions with an ArrayList of structures.</li></ol>

<div class="references_section">