Esempio n. 1
0
        private void SetDataConnection(object newDataSource, BindingMemberInfo newDisplayMember, bool force) {
            bool dataSourceChanged = dataSource != newDataSource;
            bool displayMemberChanged = !displayMember.Equals(newDisplayMember);

            // make sure something interesting is happening...
            //
            //force = force && (dataSource != null || newDataSource != null);
            if (inSetDataConnection) {
                return;
            }
            try {
                if (force || dataSourceChanged || displayMemberChanged) {
                    inSetDataConnection = true;
                    IList currentList = this.DataManager != null ? this.DataManager.List : null;
                    bool currentManagerIsNull = this.DataManager == null;

                    UnwireDataSource();

                    dataSource = newDataSource;
                    displayMember = newDisplayMember;

                    WireDataSource();

                    // Provided the data source has been fully initialized, start listening to change events on its
                    // currency manager and refresh our list. If the data source has not yet been initialized, we will
                    // skip this step for now, and try again later (once the data source has fired its Initialized event).
                    //
                    if (isDataSourceInitialized) {

                        CurrencyManager newDataManager = null;
                        if (newDataSource != null && BindingContext != null && !(newDataSource == Convert.DBNull)) {
                            newDataManager = (CurrencyManager)BindingContext[newDataSource, newDisplayMember.BindingPath];
                        }

                        if (dataManager != newDataManager) {
                            if (dataManager != null) {
                                dataManager.ItemChanged -= new ItemChangedEventHandler(DataManager_ItemChanged);
                                dataManager.PositionChanged -= new EventHandler(DataManager_PositionChanged);
                            }

                            dataManager = newDataManager;

                            if (dataManager != null) {
                                dataManager.ItemChanged += new ItemChangedEventHandler(DataManager_ItemChanged);
                                dataManager.PositionChanged += new EventHandler(DataManager_PositionChanged);
                            }
                        }

                        // See if the BindingField in the newDisplayMember is valid
                        // The same thing if dataSource Changed
                        // "" is a good value for displayMember
                        if (dataManager != null && (displayMemberChanged || dataSourceChanged) && displayMember.BindingMember != null && displayMember.BindingMember.Length != 0) {

                            if (!BindingMemberInfoInDataManager(displayMember))
                                throw new ArgumentException(SR.ListControlWrongDisplayMember, "newDisplayMember");
                        }

                        if (dataManager != null && (dataSourceChanged || displayMemberChanged || force)) {
                            // if we force a new data manager, then change the items in the list control
                            // only if the list changed or if we go from a null dataManager to a full fledged one
                            // or if the DisplayMember changed
                            if (displayMemberChanged || (force && (currentList != this.dataManager.List || currentManagerIsNull))) {
                                DataManager_ItemChanged(dataManager, new ItemChangedEventArgs(-1));
                            }
                        }
                    }
                    this.displayMemberConverter = null;
                }

                if (dataSourceChanged) {
                    OnDataSourceChanged(EventArgs.Empty);
                }

                if (displayMemberChanged) {
                    OnDisplayMemberChanged(EventArgs.Empty);
                }
            }
            finally {
                inSetDataConnection = false;
            }
        }