13.07.2015 Views

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

Applied XML Programming for Microsoft .NET.pdf - Csbdu.in

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

want a more compact <strong>for</strong>mat, opt <strong>for</strong> the b<strong>in</strong>ary .<strong>NET</strong> Framework <strong>for</strong>matter andconsider us<strong>in</strong>g a ghost class, as described <strong>in</strong> Chapter 9.Review<strong>in</strong>g and Reject<strong>in</strong>g ChangesUsers of the sample application enter changes through the <strong>in</strong>terface of the DataGridcontrol. Each change is detected, and controls <strong>in</strong> the user <strong>in</strong>terface are enabled anddisabled to reflect those changes. For example, the Review Changes button is enabledif there are changes to review.Detect<strong>in</strong>g Ongo<strong>in</strong>g ChangesIn a W<strong>in</strong>dows Forms application, data sources associated with data-bound controls aremanaged by a special breed of component—the b<strong>in</strong>d<strong>in</strong>g manager.B<strong>in</strong>d<strong>in</strong>gManagerBase is the abstract class <strong>for</strong> b<strong>in</strong>d<strong>in</strong>g managers; the actual classes youwill work with are CurrencyManager and PropertyManager.The PropertyManager class keeps track of a simple b<strong>in</strong>d<strong>in</strong>g between a data-boundcontrol property and a data source scalar value. The CurrencyManager class plays amore sophisticated role. CurrencyManager handles complex data b<strong>in</strong>d<strong>in</strong>g andma<strong>in</strong>ta<strong>in</strong>s b<strong>in</strong>d<strong>in</strong>gs between a data source and all the list controls (<strong>for</strong> example, theDataGrid control) that b<strong>in</strong>d to it or to one of its member tables. The CurrencyManagerclass takes care of synchroniz<strong>in</strong>g the controls bound to the same data source andprovides a uni<strong>for</strong>m <strong>in</strong>terface <strong>for</strong> clients to access the current item <strong>for</strong> the list. Bothmanager classes have a property named Current and fire position-related events suchas ItemChanged. The Current property returns the currently selected item, whateverthat is <strong>for</strong> the particular b<strong>in</strong>d<strong>in</strong>g class. For example, <strong>for</strong> the DataGrid class, the currentitem is the nth bound element—that is, a DataRow object if a DataTable is bound, or astr<strong>in</strong>g if an array of str<strong>in</strong>gs is bound.To access the b<strong>in</strong>d<strong>in</strong>g manager <strong>for</strong> a particular data source, you use the Form object'sB<strong>in</strong>d<strong>in</strong>gContext collection, as shown here:CurrencyManager m_bmbEmployees;m_bmbEmployees = (CurrencyManager) B<strong>in</strong>d<strong>in</strong>gContext[m_dataSet,"Employees"];m_bmbEmployees.ItemChanged +=new ItemChangedEventHandler(CurrentChanged);This code also registers a handler <strong>for</strong> the ItemChanged event. The b<strong>in</strong>d<strong>in</strong>g managerautomatically fires the event whenever an item <strong>in</strong> the bound data source—theEmployees table <strong>in</strong> the grid's DataSet object—changes. In other words, the handlerexecutes whenever a change occurs and refreshes the application's user <strong>in</strong>terfaceaccord<strong>in</strong>gly.Select<strong>in</strong>g Changed RowsAs mentioned, the DataSet object registers all the changes but reta<strong>in</strong>s the orig<strong>in</strong>alvalues of the modified rows. Thanks to these features, sett<strong>in</strong>g up a <strong>for</strong>m to review thecurrent changes is not at all difficult. Let's see how to proceed.The idea is to create a view of the table—possibly a copy of the table that <strong>in</strong>cludes onlythe changes. The GetChanges method can be used to obta<strong>in</strong> a copy of the DataTableobject (or the DataSet object) that <strong>in</strong>cludes only the changed rows, as shown here:DataTable dtChanges =m_dataSet.Tables["Employees"].GetChanges();if (dtChanges == null)return;DataView dv = dtChanges.DefaultView;381

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!