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:
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.
- 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:
Collapse this tableExpand this table
| Button | (Name) | Text |
|---|
| Button1 | cmdFirst | << |
| Button2 | cmdPrevious | < |
| Button3 | cmdNext | > |
| Button4 | cmdLast | >> |
| Button5 | cmdCancelEdit | Undo |
- 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;
- 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);
- 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");
}
- 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;
- 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;
}
- 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;
}
- 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;
- 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();
- Modify the connection string to use your SQL Server and log on to connect.
- Press F5 to build and then run the project.
- Click the buttons to browse between the rows. This raises the CurrentChanged event and the PositionChanged event.
- 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.
- 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:
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
| 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