Microsoft Knowledge Base Email Alertz

KBAlertz.com: (312045) - 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...

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]

Search KbAlertz

Advanced Search

Webmasters
Put kbAlertz on your website.
[ Click Here for more! ]





ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **


Bug Tracking Software
For bug tracking software or defect tracking software or issue tracking software, visit Axosoft.


Community Site



We Send hundreds of thousands of emails using ASP.NET Email



Expert Web Design & Graphic Design
Design44.com

ASP.NET 3.5 Web Hosting with Windows 2008 and SQL 2008: Click Here!
Discount ASP.NET Hosting
ASP.NET 2.0 and 3.5
Windows2008 and SQL2008
US and UK Hosting
The ad says 3 - but KBAlertz referrals get
** SIX MONTHS FREE **




Mentioned In








Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks




Article ID: 312045 - Last Review: May 13, 2007 - Revision: 3.5

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

This article was previously published under Q312045
For a Microsoft Visual Basic .NET version of this article, see 311543  (http://kbalertz.com/Feedback.aspx?kbNumber=311543/ ) .

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

On This Page

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.

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.

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.

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:
    Collapse this tableExpand this table
    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.

REFERENCES

For additional information, click the following article number to view the article in the Microsoft Knowledge Base:
308484  (http://kbalertz.com/Feedback.aspx?kbNumber=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:
Consumers of data on Windows Forms
http://msdn2.microsoft.com/en-us/library/aa983578(VS.71).aspx (http://msdn2.microsoft.com/en-us/library/aa983578(VS.71).aspx)

CurrencyManager class
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager.aspx (http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager.aspx)

CurrencyManager events
http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager_events.aspx (http://msdn.microsoft.com/en-us/library/system.windows.forms.currencymanager_events.aspx)

APPLIES TO
  • Microsoft ADO.NET (included with the .NET Framework)
  • Microsoft ADO.NET 1.1
  • Microsoft Visual C# .NET 2002 Standard Edition
  • Microsoft Visual C# .NET 2003 Standard Edition
Keywords: 
kbdatabase kbdatabinding kbinfo kbsqlclient kbsystemdata KB312045
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate

Be the first to leave feedback, to help others about this knowledge base article.

(Optional) Name

(Optional) Public URL Or Email

Comments
No HTML -- Text Only Please