Microsoft KB Archive/315784

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

Article ID: 315784

Article Last Modified on 5/11/2007

-

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 Q315784



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



IN THIS TASK
SUMMARY Requirements
 * Design the Class
 * Add Class Instances to an Array
 * Bind the Array to the DataGrid Control
 * Provide a Means to Browse the Array

Step-by-Step Example
 * Use a Structure Instead of a Class

REFERENCES



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

back to the top

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

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

back to the top

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 back to the top

Add Class Instances to an Array

 * 1) Declare an array.
 * 2) Create instances of the class, and then add the instances to the array.

Private arr(2) As guitar

arr(0) = New guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) arr(1) = New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) arr(2) = New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971) back to the top

Bind the Array to the DataGrid Control
After the array has been populated, set the DataSource property of the DataGrid control to the array. The columns in the DataGrid control are populated based on the properties for which in-scope property accessors exist. DataGrid1.DataSource = arr back to the top

Provide Means to Browse the Array
You can use CurrencyManager to browse through the array. To do this, associate CurrencyManager with the BindingContext of the control (in this case, the array). Private cManager As CurrencyManager

cManager = CType(DataGrid1.BindingContext(arr), CurrencyManager) The CurrencyManager class has a Position property that you can manipulate to iterate over the members of the array. 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 = arr.Length - 1 back to the top

Step-by-Step Example
 In Visual Basic .NET or Visual Basic 2005, create a new Windows Application project. Form1 is created by default.

Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

 On the Project menu, select 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 arr(2) As guitar </li>  Add the following code to the Form1_Load event: arr(0) = New guitar(&quot;Gibson&quot;, &quot;Les Paul&quot;, 1958) arr(1) = New guitar(&quot;Fender&quot;, &quot;Jazz Bass&quot;, 1964) arr(2) = New guitar(&quot;Guild&quot;, &quot;Bluesbird&quot;, 1971) cManager = CType(DataGrid1.BindingContext(arr), CurrencyManager) DataGrid1.DataSource = arr Note In Visual Studio 2005, you can use the generic type to implement these code. For more information, visit the following Microsoft Developer (MSDN) Web site:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/vb2005_generics.asp

</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 = arr.Length - 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>

back to the top

Use 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 array 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 array of structures.</li></ol>

back to the top

<div class="references_section">