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
- Declare an array.
- Create instances of the class, and then add the instances to the array.
Private arr(2) As guitar
arr(0) = New guitar("Gibson", "Les Paul", 1958)
arr(1) = New guitar("Fender", "Jazz Bass", 1964)
arr(2) = New guitar("Guild", "Bluesbird", 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:
For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:
- 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.
- Change the Text property of Button1 to Next.
- Change the Text property of Button2 to Previous.
- Change the Text property of Button3 to First.
- Change the Text property of Button4 to Last.
- Add the following code to the Form1 class:
Private cManager As CurrencyManager
Private arr(2) As guitar
- Add the following code to the Form1_Load event:
arr(0) = New guitar("Gibson", "Les Paul", 1958)
arr(1) = New guitar("Fender", "Jazz Bass", 1964)
arr(2) = New guitar("Guild", "Bluesbird", 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: - 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
- Build and run the project.
- 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.
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:
- 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
- Build and run the example program again, and verify that it functions with an array of structures.
back to the top