Microsoft KB Archive/316303

= How to bind a DataGrid control to an ArrayList of objects or structures by using Visual C# =

Article ID: 316303

Article Last Modified on 12/11/2006

-

APPLIES TO


 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# 2005 Express Edition

-



This article was previously published under Q316303



For a Microsoft Visual Basic .NET version of this article, see 316302.

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

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

REFERENCES



SUMMARY
This step-by-step article shows you how to bind an ArrayList 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 C# .NET or Visual C# 2005

This article assumes that you are familiar with the following topics:
 * Visual C# 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 string make; private string model; private short year; public guitar {   }

public guitar(string make, string model, short year) {       make=Make; model=Model; year=Year; }

public string Make {   get {            return make; }   set {           make = value; }

} back to the top

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 ArrayList al = 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)); back to the top

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

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 CurrencyManager currencyManager=null;

currencyManager = (CurrencyManager)dataGrid1.BindingContext[al]; 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. currencyManager.Position++; //Move back one element. currencyManager.Position--; //Move to the beginning. currencyManager.Position = 0; //Move to the end. currencyManager.Position = al.Count - 1; back to the top

Step-by-Step Example
 In Visual C# .NET or Visual C# 2005, create a new Windows Application project. Form1 is created by default. Add a class to the project.  Replace the code in Class1.cs with the following: public class guitar {   private string make; private string model; private short year; public guitar {   }

public guitar(string Make, string Model, short Year) {       make=Make; model=Model; year=Year; }

public string Make {       get {            return make; }       set {           make = value; }   }    public string Model {       get {            return model; }       set {           model = value; }   }

public short Year {       get {            return year; }       set {           year = value; }   } }                     Close the Class1.cs 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.</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 ArrayList al = new ArrayList; private CurrencyManager currencyManager=null; </li> Switch to the Form Designer, right-click the form, and then click Properties.</li> Click the Events icon, and then double-click the Load event to add the Form1_Load event to your code.</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)); currencyManager = (CurrencyManager)dataGrid1.BindingContext[al]; dataGrid1.DataSource=al; </li> Switch to view the Form Designer.</li>  Double-click Next, and then add the following code to the button1_Click event: currencyManager.Position++; </li>  Double-click Previous, and then add the following code to the button2_Click event: currencyManager.Position--; </li>  Double-click First, and then add the following code to the button3_Click event: currencyManager.Position = 0; </li>  Double-click Last, and then add the following code to the button4_Click event: currencyManager.Position = al.Count - 1; </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>

Note The code should be changed in Visual Studio 2005. When you create a Windows Forms project, Visual C# adds one form to the project by default. This form is named Form1. The two files that represent the form are named Form1.cs and Form1.designer.cs. You write your code in Form1.cs. The Designer.cs file is where the Windows Forms Designer writes the code that implements all the actions that you performed by adding controls. For more information about the Windows Forms Designer in Visual C# 2005, visit the following Microsoft Web site:

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

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 ArrayList of structures, follow these steps: <ol>  Change the definition of the Class1.cs class module in the example from public class guitar to the following: public struct guitar </li> <li> Comment out the default constructor, as follows: //public guitar //{ //}                   </li> <li>Build and run the example program again, and verify that it functions with an ArrayList of structures.</li></ol>

back to the top

<div class="references_section">