Microsoft KB Archive/815676

= How to display parent and child fields together in a Windows Forms DataGrid by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815676

Article Last Modified on 5/12/2007

-

APPLIES TO


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

-





For a Microsoft Visual C# .NET version of this article, see 308484.



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

This article refers to the following Microsoft .NET Framework Class Library namespace:
 * System.Data.SqlClient



IN THIS TASK

 * SUMMARY
 * Requirements
 * Understand the CurrentManager object
 * Create the project
 * Test the application
 * REFERENCES



SUMMARY
This step-by-step article describes how to create a Windows form that displays a parent (or master) record and all the related child (or detail) records by using the Northwind Customers and Orders tables. This article also describes the CurrencyManager object and its purpose.

In this article, the parent record information appears in TextBox controls, and the child record information appears in a DataGrid control. The project that you create in this article also contains Button controls so that you can browse through the records.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, Windows 2000 Advanced Server, or Windows NT 4.0 Server
 * Microsoft SQL Server 7.0 or 2000, or Microsoft Data Engine (MSDE) with the Northwind sample database installed
 * Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005

back to the top

Understand the CurrencyManager object
Make sure that you understand the function of the CurrencyManager object to help you follow the sample in this article. Any data source that you bind to a Windows Forms control contains an associated CurrencyManager object. The CurrencyManager object monitors the position and otherwise supervises bindings to that data source. The form contains a CurrencyManager object for each discrete data source to which you are binding. If all of the controls on the form bind to a single source (for example, if several TextBox controls are bound to the same data table), all the controls share the same CurrencyManager object. When the controls on the form are bound to different sources, the form contains multiple CurrencyManager objects. Each of these CurrencyManager objects tracks the record or the data element that the controls are using.

back to the top

Create the project
To create the project that demonstrates the functionality of the CurrencyManager object, follow these steps:  Start Visual Studio .NET 2003 or Visual Studio 2005, and then create a new Microsoft Visual C++ Windows Forms Application (.NET) project.

Note In Visual Studio 2005, create a new Microsoft Visual C++ Windows Forms Application project. Add three TextBox controls, five Button controls, and one DataGrid control to the form. Change the Text properties of the Button controls to Fill, First , Next , Previous , and Last .  Switch to Code view, and add the following statement to the top of the Code window: using namespace System::Data::SqlClient;   Add the following member variable to the Form1 class after the private statements: CurrencyManager * cm;   Switch to Form view, and then double-click Fill. Add the following code to the Click event: {// Get the data. SqlConnection * con = new SqlConnection(&quot;server=YourServer; uid=YourUserID;pwd=YourPassword;database=northwind&quot;); SqlDataAdapter * daCust = new SqlDataAdapter(&quot;Select * from Customers&quot;, con); SqlDataAdapter * daOrders = new SqlDataAdapter(&quot;Select * from Orders&quot;,con); DataSet * ds=new DataSet; daCust->Fill(ds,&quot;Cust&quot;); daOrders->Fill(ds,&quot;Orders&quot;);

ds->Relations->Add(&quot;CustOrd&quot;,ds->Tables->get_Item(&quot;Cust&quot;)->Columns->get_Item(&quot;CustomerID&quot;), ds->Tables->get_Item(&quot;Orders&quot;)->Columns->get_Item(&quot;CustomerID&quot;));

// //Bind the controls. // textBox1->DataBindings->Add(&quot;Text&quot;, ds->Tables->get_Item(&quot;Cust&quot;),&quot;CustomerID&quot;); textBox2->DataBindings->Add(&quot;Text&quot;, ds->Tables->get_Item(&quot;Cust&quot;),&quot;CompanyName&quot;); textBox3->DataBindings->Add(&quot;Text&quot;, ds->Tables->get_Item(&quot;Cust&quot;),&quot;ContactName&quot;); dataGrid1->DataSource=ds->Tables->get_Item(&quot;Cust&quot;); dataGrid1->DataMember=&quot;CustOrd&quot;; // //Initialize the CurrencyManager. // try{ cm=__try_cast<CurrencyManager *>(this->BindingContext->get_Item(ds->Tables->get_Item(&quot;Cust&quot;))); } catch(Exception * ex) {   MessageBox::Show(ex->get_Message,S&quot;Exception&quot;); }}              Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

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

These steps apply to the whole article. </li> Modify the connection string as is appropriate for your environment.</li>  Double-click First, and then add the following code to the Click event: {if((cm!=NULL)&(cm->Count>0)) cm->Position=0;} </li>  Double-click Next, and then add the following code to the Click event: {if((cm!=NULL)&(cm->Count>0)&(cm->Position<cm->Count-1)) cm->Position +=1;} </li>  Double-click Previous, and then add the following code to the Click event: {if((cm!=NULL)&(cm->Count>0)&(cm->Position>0)) cm->Position -=1;}  </li>  Double-click Last, and then add the following code to the Click event: {if((cm!=NULL)& (cm->Count>0)) cm->Position=cm->Count;} </li></ol>

back to the top

Test the application

 * 1) Press F5 to compile and to run the application. Notice that the form is initially empty.
 * 2) Click Fill to load and to bind the data, and then use the navigation buttons to move through the data.

back to the top

<div class="references_section">