Microsoft KB Archive/312045

= Firing the Windows Forms CurrencyManager events in Visual C# .NET =

Article ID: 312045

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1 Service Pack 1
 * Microsoft ADO.NET 1.1
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition

-



This article was previously published under Q312045





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

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



SUMMARY
The CurrencyManager object allows you to synchronize bound controls as a user browses through rows in a table. For example, CurrencyManager allows you to display the correct FirstName and LastName properties in separate, bound TextBox controls as a user browses through the Employees table.

CurrencyManager has three events: CurrentChanged, ItemChanged, and PositionChanged. You can use these events to track when a user moves through rows and when a user edits values in bound controls. This article describes which actions cause the various CurrencyManager events to fire.

This article is divided into the following sections:
 * CurrentChanged event
 * ItemChanged event
 * PositionChanged event
 * Sample code
 * References

The &quot;Sample code&quot; section demonstrates the firing of the CurrencyManager events.



CurrentChanged event
The CurrentChanged event of the CurrencyManager fires when the bound value of the current row in the CurrencyManager list of rows changes.

Specifically, the CurrentChanged event of the CurrencyManager fires in the following situations:
 * When the user changes position in the CurrencyManager list of rows so that a different value is in the bound control.
 * When the user changes the bound value in the current row of the CurrencyManager.
 * When the user adds a row in a position that is lower than the current position in the CurrencyManager list.
 * When the user deletes the current row in the CurrencyManager list.
 * When the user deletes a row that is located before the current position in the list.
 * When the user refreshes the list, such as by sorting.
 * When the user moves the current row in the CurrencyManager to another position in the list.
 * When the user moves a row to the current position in the list.
 * When the DataSource property of the CurrencyManager is changed.

back to the top

ItemChanged event
The ItemChanged event of the CurrencyManager fires when any item in any CurrencyManager row is altered directly through the data source. The ItemChanged event does not fire when the value is changed through the user interface (UI).

Specifically, the ItemChanged event of the CurrencyManager fires in the following situations:
 * When the user calls the CancelCurrentEdit method of the CurrencyManager.
 * When the user adds a row to the CurrencyManager list.
 * When the user deletes any row from the CurrencyManager list.
 * When the user updates any row in the CurrencyManager list.
 * When the user refreshes the CurrencyManager list, such as by sorting.
 * When the DataSource property of the CurrencyManager is changed.

back to the top

PositionChanged event
The PositionChanged event fires when the Position property of the CurrencyManager list changes. The Position property is the current position in CurrencyManager list of rows.

Specifically, the PositionChanged event of the CurrencyManager fires in the following situations:
 * When the user moves to a different row, which implicitly changes the Position property of the CurrencyManager.
 * When the Position property of the CurrencyManager is changed programmatically.

back to the top

Sample code
This sample code displays message boxes when the CurrencyManager events are fired. The code uses the Employees table of the Microsoft SQL Server Northwind database.  Create a new Visual C# Windows Application project. By default, Form1 is added to the project. Add three TextBox controls to Form1. Textbox1, Textbox2, and Textbox3 are added by default. Add five Button controls to Form1. Button1 through Button5 are added by default. Change the Button properties as follows:   Open the Code window for Form1. Paste the following code at the very top of the Code window in the section that contains the using statements: using System.Data; using System.Data.SqlClient;   The CurrencyManager must be defined in form-level scope so that it is available in each button. Paste the following code immediately before the class declaration public Form1: private CurrencyManager myCurrencyManager; </li>  Add the following code to the Form1_Load event procedure. This code opens the Employees table and then binds the text boxes to the EmployeeID, FirstName, and LastName properties: // Open a connection to the SQL Server Northwind database. // Modify the following line to connect with your SQL Server and log on. SqlConnection con = new SqlConnection(&quot;server=server;uid=login;pwd=password;                                     database=northwind&quot;);

// Open the Employees table. SqlDataAdapter daCust = new SqlDataAdapter(&quot;Select * from Employees&quot;, con);

// Store the Employees into a local table. DataSet ds = new DataSet; daCust.Fill(ds, &quot;Employees&quot;);

// Bind the TextBox controls to the fields of the Employees table. textBox1.DataBindings.Add(&quot;Text&quot;, ds.Tables[&quot;Employees&quot;], &quot;EmployeeID&quot;); textBox2.DataBindings.Add(&quot;Text&quot;, ds.Tables[&quot;Employees&quot;], &quot;FirstName&quot;); textBox3.DataBindings.Add(&quot;Text&quot;, ds.Tables[&quot;Employees&quot;], &quot;LastName&quot;);

// Specify the CurrencyManager for the Employees table. myCurrencyManager = (CurrencyManager)this.BindingContext[ds.Tables[&quot;Employees&quot;]];

// Specify the event handlers for the CurrencyManager. myCurrencyManager.CurrentChanged += new EventHandler(Current_Changed); myCurrencyManager.ItemChanged += new ItemChangedEventHandler(CurrencyManager_ItemChanged); myCurrencyManager.PositionChanged += new EventHandler(Position_Changed); </li>  Paste the following code immediately after the Form1_Load event procedure. This code declares the CurrencyManager events and displays message boxes as each event fires: private void Current_Changed(object sender, EventArgs e) { MessageBox.Show(&quot;Current_Changed&quot;); }

private void CurrencyManager_ItemChanged(object sender, System.Windows.Forms.ItemChangedEventArgs e){ MessageBox.Show(&quot;Item_Changed&quot;); }

private void Position_Changed(object sender, EventArgs e){ MessageBox.Show(&quot;Position_Changed&quot;); }                   </li>  Double-click cmdFirst to open its Code window. Paste the following code into the cmdFirst_Click event procedure: // Move to the first row in the DataTable. myCurrencyManager.Position = 0; </li>  Double-click cmdPrevious to open its Code window. Paste the following code into the cmdPrevious_Click event procedure: // If you are positioned past the first row, if (myCurrencyManager.Position > 0) { // move back one row. myCurrencyManager.Position -= 1; }                   </li>  Double-click cmdNext to open its Code window. Paste the following code into the cmdNext_Click event procedure: // If you are positioned before the last row, if (myCurrencyManager.Position < myCurrencyManager.Count) { //move back one row. myCurrencyManager.Position += 1; }                   </li>  Double-click cmdLast to open its Code window. Paste the following code into the cmdLast_Click event procedure: // Position at the last row. myCurrencyManager.Position = myCurrencyManager.Count; </li>  Double-click cmdCancelEdit to open its Code window. Paste the following code into the cmdCancelEdit_Click event procedure: // Cancel the current editing in the bound controls. myCurrencyManager.CancelCurrentEdit; </li> Modify the connection string to use your SQL Server and log on to connect.</li> Press F5 to build and then run the project.</li> Click the buttons to browse between the rows. This raises the CurrentChanged event and the PositionChanged event.</li> Modify the value in either the FirstName box or the LastName box, and then move to a different row. This raises the CurrentChanged event and the PositionChanged event.</li> Modify a value, and then click Undo to cancel the change. This raises the ItemChanged event.</li></ol>

back to the top

<div class="references_section">