Firing the Windows Forms CurrencyManager events in Visual C# .NET (312045)



The information in this article applies to:

  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET (included with the .NET Framework 1.1)
  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)

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: The "Sample code" section demonstrates the firing of the CurrencyManager events.

MORE INFORMATION

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.
  1. Create a new Visual C# Windows Application project. By default, Form1 is added to the project.
  2. Add three TextBox controls to Form1. Textbox1, Textbox2, and Textbox3 are added by default.
  3. Add five Button controls to Form1. Button1 through Button5 are added by default.
  4. Change the Button properties as follows:
    Button(Name)Text
    Button1cmdFirst<<
    Button2cmdPrevious<
    Button3cmdNext>
    Button4cmdLast>>
    Button5cmdCancelEditUndo
  5. 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;
    					
  6. 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;
    					
  7. 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("server=server;uid=login;pwd=password;
                                          database=northwind");
    
    // Open the Employees table.
    SqlDataAdapter daCust = new SqlDataAdapter("Select * from Employees", con);
    
    // Store the Employees into a local table.
    DataSet ds = new DataSet();
    daCust.Fill(ds, "Employees");
    
    // Bind the TextBox controls to the fields of the Employees table.
    textBox1.DataBindings.Add("Text", ds.Tables["Employees"], "EmployeeID");
    textBox2.DataBindings.Add("Text", ds.Tables["Employees"], "FirstName");
    textBox3.DataBindings.Add("Text", ds.Tables["Employees"], "LastName");
    
    // Specify the CurrencyManager for the Employees table.
    myCurrencyManager = (CurrencyManager)this.BindingContext[ds.Tables["Employees"]];
    
    // 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);
    					
  8. 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("Current_Changed");
    }
    
    private void CurrencyManager_ItemChanged(object sender, 
    System.Windows.Forms.ItemChangedEventArgs e){
        MessageBox.Show("Item_Changed");
    }
    
    private void Position_Changed(object sender, EventArgs e){
        MessageBox.Show("Position_Changed");
    }
    					
  9. 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;
    					
  10. 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;
    }
    					
  11. 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;
    }
    					
  12. 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;
    					
  13. 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();
    					
  14. Modify the connection string to use your SQL Server and log on to connect.
  15. Press F5 to build and then run the project.
  16. Click the buttons to browse between the rows. This raises the CurrentChanged event and the PositionChanged event.
  17. 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.
  18. Modify a value, and then click Undo to cancel the change. This raises the ItemChanged event.
back to the top

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:

308484 How to display parent and child records in a Windows Forms DataGrid by using Visual C# .NET

For more information, see the following topics in Visual Studio .NET Help documentation: back to the top

Modification Type:MajorLast Reviewed:9/28/2004
Keywords:kbDatabase kbDataBinding kbinfo kbSqlClient kbSystemData KB312045