How to Bind a GridView to a Collection
A collection class lets you create a list of related values. These values contain properties that define the type of value in the collection. You can use .NET collections to populate the .NET GridView control. The GridView automatically parses the data in the collection and displays the data in a list of rows and columns.
Instructions
-
-
1
Click the Windows "Start" button and select "All Programs." Click "Microsoft .NET," then click "Visual Studio." Open your .NET project that contains the collection and GridView.
-
2
Double-click the .NET form you want to use to display the collection data. If you do not already have a GridView created on the form, drag and drop a GridView from the toolbox to the form.
-
-
3
Right-click the form and select "View Code." The .NET code editor opens. Add the following code to the top of the editor to include support for the collection:
Imports System.Collections.Generic
-
4
Create the collection data. Collections are reusable code you must populate with values before you bind the collection to a GridView. The following code adds a list of values to a "Customer" collection:
Dim customerlist As Vehicles = New Customer()
customerlist.Add(New Customer("Name", "Joe"))
customerlist.Add(New Customer("Address", "111 Nowhere St"))
-
5
Bind the collection to the GridView. The following code adds the collection to the GridView:
grid.DataSource = customerlist
grid.DataBind()
-
1