Microsoft KB Archive/816146

= How to bind a DataGrid control to an array of objects or an array of structures by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 816146

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ 2005 Express Edition

-







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



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 includes a Windows form with a DataGrid control to display object property values, and includes 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:
 * Microsoft Visual C++ .NET or Microsoft 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 you bind to a control must have property accessors. Any property that you bind must have the Property Set and the Property Get methods. The sample class that is used in this article has three members. A parameterized constructor is also provided. However, a parameterized constructor is not required. __gc class Guitar { private: String *make; String *model; short year; public: Guitar(void); ~Guitar(void); Guitar(String *strMake, String *strModel, short shYear) {           make = strMake; model = strModel; year = shYear; }

__property String* get_Make {           return make; }       __property void set_Make(String* val) {           make = (val); }       __property String* get_Model {            return model; }       __property void set_Model(String* val) {           model = (val); }

__property short get_Year {            return year; }       __property void set_Year(short shValue) {           year = shValue; } }; Note You must add the common language runtime (CLR) support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample.

To do this, follow these steps:
 * 1) Click Project, and then click   Properties.

Note  represents the name of the project.
 * 1) Expand Configuration Properties, and then click General.
 * 2) Click to select the Common Language Runtime Support, Old Syntax (/clr:oldSyntax) check box under the Common Language Runtime support in the right pane, click Apply, and then click OK.

For more information about the CLR support compiler options, visit the following Microsoft Web site:

/clr (Common language runtime compilation)

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

back to the top

Add class instances to an array
To create instances, and then add them to the array, follow these steps:  Declare an array.  Create instances of the class, and then add the instances to the array as follows: private: Guitar *arr[];

arr = new Guitar *[3]; arr[0] = new Guitar(S&quot;Gibson&quot;, S&quot;Les Paul&quot;, 1958); arr[1] = new Guitar(S&quot;Fender&quot;, S&quot;Jazz Bass&quot;, 1964); arr[2] = new Guitar(S&quot;Guild&quot;, S&quot;Bluesbird&quot;, 1971); 

back to the top

Bind the array to the DataGrid control
After the array is 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 a means to browse the array
You can use the CurrencyManager class to browse through the array. To do this, associate the CurrencyManager class with the BindingContext property of the control (in this case, the array). private: CurrencyManager *currencyManager; currencyManager = NULL; currencyManager = static_cast(dataGrid1->BindingContext->get_Item(arr)); The CurrencyManager class has a Position property that you can change to iterate over the members of the array. By adding to or subtracting from the current value of the Position property, 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 = arr->Length - 1; back to the top

Step-by-step example
 Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project.</li> Click Visual C++ .NET Projects under Project Types, and then click Windows Forms Application (.NET) under Templates.

Note In Visual Studio 2005, click Visual C++ under Project Types, and then click Windows Forms Application under Templates.</li> In the Name text box, type Q816146. In the Locate text box, type C:\, and then click OK .</li> In Solution Explorer, switch to Class View. To do this, click Class View on the View menu.</li> Right-click Q816146, point to Add, and then click Add Class.</li> In the Add Class dialog box, click Generic C++ Class under Templates, and then click Open.

Note In Visual Studio 2005, click Add.</li>  In the Generic C++ Class Wizard, type Guitar in the Class name text box, and then click Finish. The Guitar class appears as follows:
 * 1) pragma once

class Guitar { public: Guitar(void); ~Guitar(void); }; To make the Guitar class a Managed Extensions for C++ class, add the __gc keyword in front of the Guitar class: __gc class Guitar { public: Guitar(void); ~Guitar(void); }; </li>  Replace the code in Guitar.h with the following:
 * 1) pragma once

using namespace System; __gc class Guitar { private: String *make; String *model; short year; public: Guitar(void); ~Guitar(void); Guitar(String *strMake, String *strModel, short shYear) {           make = strMake; model = strModel; year = shYear; }

__property String* get_Make {           return make; }       __property void set_Make(String* val) {           make = (val); }       __property String* get_Model {            return model; }       __property void set_Model(String* val) {           model = (val); }

__property short get_Year {            return year; }       __property void set_Year(short shValue) {           year = shValue; } }; </li> Close the Guitar.h code window, and then switch to the Form Designer.</li> Add a DataGrid control to Form1. Size the DataGrid control to contain four columns and three rows.</li> 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>  Open the Form1.h file, and add the following code:  Add the following code to the Form1 class: private: Guitar *arr[]; CurrencyManager *currencyManager; </li> <li>Switch to the Form Designer, right-click the form, and then click Properties.</li> <li>Click the Events icon, and then double-click the Load event to add the Form1_Load event to your code.</li> <li> Add the following code to the Form1_Load event: currencyManager = NULL; arr = new Guitar *[3]; arr[0] = new Guitar(S&quot;Gibson&quot;, S&quot;Les Paul&quot;, 1958); arr[1] = new Guitar(S&quot;Fender&quot;, S&quot;Jazz Bass&quot;, 1964); arr[2] = new Guitar(S&quot;Guild&quot;, S&quot;Bluesbird&quot;, 1971);
 * 1) include &quot;Guitar.h&quot; </li>

currencyManager = static_cast<CurrencyManager*>(dataGrid1->BindingContext->get_Item(arr)); dataGrid1->DataSource = arr; </li> <li>Switch to the Form Designer.</li> <li> Double-click Next, and then add the following code to the button1_Click event: currencyManager->Position++; </li> <li> Double-click Previous, and then add the following code to the button2_Click event: currencyManager->Position--; </li> <li> Double-click First, and then add the following code to the button3_Click event: currencyManager->Position = 0; </li> <li> Double-click Last, and then add the following code to the button4_Click event: currencyManager->Position = arr->Length - 1; </li> <li>Build and run the project.</li> <li>Click the command buttons to move among the rows of the DataGrid control. You can edit the values of the objects.</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. You must have property (or member) accessors to do this. A structure that is created for this purpose looks similar to a class.

To bind to an array of structures, follow these steps: <ol> <li> Change the definition of the Class1.cs class module in the example from the following: __gc class Guitar to the following: __gc struct Guitar </li> <li>Build and run the example program again. Verify that it functions with an array of structures.</li></ol>

back to the top

Additional query words: Bind, DataGrid, Array of Objects, Array of Structures

<div class="references_section">