DataTableReaderListener.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ 4.0 / 4.0 / untmp / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / Data / System / Data / DataTableReaderListener.cs / 1305376 / DataTableReaderListener.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
// [....] 
// [....]
//----------------------------------------------------------------------------- 
 
namespace System.Data {
    using System; 
    using System.Data.Common;
    using System.Collections;
    using System.ComponentModel;
 
    internal sealed class DataTableReaderListener {
 
        private DataTable currentDataTable = null; 
        private bool  isSubscribed = false;
        private WeakReference readerWeak; 

        internal DataTableReaderListener(DataTableReader reader) {
            if (reader == null)
                throw ExceptionBuilder.ArgumentNull("DataTableReader"); 
            if (currentDataTable != null) {
                UnSubscribeEvents(); 
            } 
            this.readerWeak = new WeakReference(reader);
            currentDataTable = reader.CurrentDataTable; 
            if (currentDataTable != null)
                SubscribeEvents();
        }
 
        internal void CleanUp() {
            UnSubscribeEvents(); 
        } 

        internal void UpdataTable(DataTable datatable) { 
            if (datatable == null)
                throw ExceptionBuilder.ArgumentNull("DataTable");

            UnSubscribeEvents(); 
            currentDataTable = datatable;
            SubscribeEvents(); 
        } 

        private void SubscribeEvents() { 
            if (currentDataTable == null)
                return;
            if (isSubscribed)
                return; 
            currentDataTable.Columns.ColumnPropertyChanged    += new CollectionChangeEventHandler(SchemaChanged);
            currentDataTable.Columns.CollectionChanged         += new CollectionChangeEventHandler(SchemaChanged); 
 
            currentDataTable.RowChanged    += new DataRowChangeEventHandler(DataChanged );
            currentDataTable.RowDeleted    += new DataRowChangeEventHandler(DataChanged); 

            currentDataTable.TableCleared    += new DataTableClearEventHandler(DataTableCleared);
            isSubscribed = true;
        } 

        private void UnSubscribeEvents() { 
            if (currentDataTable == null) 
                return;
            if (!isSubscribed) 
                return;

            currentDataTable.Columns.ColumnPropertyChanged    -= new CollectionChangeEventHandler(SchemaChanged);
            currentDataTable.Columns.CollectionChanged         -= new CollectionChangeEventHandler(SchemaChanged); 

            currentDataTable.RowChanged    -= new DataRowChangeEventHandler(DataChanged ); 
            currentDataTable.RowDeleted    -= new DataRowChangeEventHandler(DataChanged); 

            currentDataTable.TableCleared    -= new DataTableClearEventHandler(DataTableCleared); 
            isSubscribed = false;
        }

 
       private void DataTableCleared(object sender, DataTableClearEventArgs e) {
           DataTableReader reader = (DataTableReader) readerWeak.Target; 
           if (reader != null) { 
               reader.DataTableCleared();
           } 
           else {
               UnSubscribeEvents();
           }
 
       }
 
       private void SchemaChanged(object sender, CollectionChangeEventArgs e) { 
           DataTableReader reader = (DataTableReader) readerWeak.Target;
           if (reader != null) { 
               reader.SchemaChanged();
           }
           else {
               UnSubscribeEvents(); 
           }
       } 
 
       private void DataChanged( object sender, DataRowChangeEventArgs args ) {
           DataTableReader reader = (DataTableReader) readerWeak.Target; 
           if (reader != null) {
               reader.DataChanged(args);
           }
           else { 
               UnSubscribeEvents();
           } 
       } 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK