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



The information in this article applies to:

  • Microsoft Visual Basic 2005
  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)

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("Gibson", "Les Paul", 1958))
al.Add(New guitar("Fender", "Jazz Bass", 1964))
al.Add(New guitar("Guild", "Bluesbird", 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

  1. In Visual Basic2005 or in Visual Basic .NET, create a new Windows Application project. Form1 is created by default.
  2. On the Project menu, click Add Class to add a class to the project.
  3. 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
    					
  4. Close the Class1.vb code window, and then switch to the Form Designer.
  5. Add a DataGrid control to Form1. Size the DataGrid control to accommodate four columns and three rows.
  6. Add four Button controls to Form1, and then arrange the buttons horizontally.
  7. Change the Text property of Button1 to Next.
  8. Change the Text property of Button2 to Previous.
  9. Change the Text property of Button3 to First.
  10. Change the Text property of Button4 to Last.
  11. Add the following code to the Form1 class:
    Private cManager As CurrencyManager
    Private al as New ArrayList()
    					
  12. Paste the following code in the Form1_Load event:
    al.Add(New guitar("Gibson", "Les Paul", 1958))
    al.Add(New guitar("Fender", "Jazz Bass", 1964))
    al.Add(New guitar("Guild", "Bluesbird", 1971))
    				
    cManager = CType(DataGrid1.BindingContext(al), CurrencyManager)
    	
    DataGrid1.DataSource = al
    					
  13. 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
    					
  14. Build and run the project.
  15. 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.

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:
  1. 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
    					
  2. Build and run the example program again, and verify that it functions with an ArrayList of structures.

REFERENCES

For more information, see the "Consumers of Data on Windows Forms" topic in the Visual Studio .NET Online Help.

Modification Type:MinorLast Reviewed:10/3/2006
Keywords:kbvs2005swept kbvs2005applies kbHOWTOmaster KB316302 kbAudDeveloper