Esempio n. 1
0
        //=====================================================================

        /// <summary>
        /// This is used to update the change policy based on the owning data list or data navigator
        /// </summary>
        /// <param name="canAdd">If true and the data source permits it, allow additions</param>
        /// <param name="canEdit">If true and the data source permits it, allow edits</param>
        /// <param name="canDelete">If true and the data source permits it, allow deletes</param>
        internal void UpdatePolicy(bool canAdd, bool canEdit, bool canDelete)
        {
            bool listCanAdd, listCanEdit, listCanDelete, listChangeNotify, oldAdd = this.AllowAdditions,
                 oldEdit = this.AllowEdits, oldDelete = this.AllowDeletes;

            CurrencyManager listManager = (dataList != null) ? dataList.ListManager : dataNav.ListManager;

            if (listManager == null)
            {
                this.AllowAdditions = canAdd;
                this.AllowEdits     = canEdit;
                this.AllowDeletes   = canDelete;
            }
            else
            {
                IBindingList bl = (listManager.List as IBindingList);

                if (bl != null)
                {
                    listCanAdd       = bl.AllowNew;
                    listCanEdit      = bl.AllowEdit;
                    listCanDelete    = bl.AllowRemove;
                    listChangeNotify = bl.SupportsChangeNotification;
                }
                else
                {
                    listCanAdd       = listCanDelete = (!listManager.List.IsReadOnly && !listManager.List.IsFixedSize);
                    listCanEdit      = !listManager.List.IsReadOnly;
                    listChangeNotify = false;
                }

                this.AllowAdditions = (canAdd && listCanAdd && listChangeNotify);
                this.AllowEdits     = (canEdit && listCanEdit);
                this.AllowDeletes   = (canDelete && listCanDelete && listChangeNotify);
            }

            // If the change policy was modified, raise the ChangePolicyModified event on the owner
            if (dataList != null)
            {
                if (this.AllowAdditions != oldAdd || this.AllowEdits != oldEdit || this.AllowDeletes != oldDelete)
                {
                    dataList.OnChangePolicyModified(new ChangePolicyEventArgs(this.AllowAdditions,
                                                                              this.AllowEdits, this.AllowDeletes));
                }
                else
                {
                    // Enable or disable Add and Delete based on the policy
                    dataList.btnAdd.Enabled    = (this.AllowAdditions && listManager != null);
                    dataList.btnDelete.Enabled = (this.AllowDeletes && listManager != null &&
                                                  listManager.Count > 0);
                }
            }
            else
            if (this.AllowAdditions != oldAdd || this.AllowEdits != oldEdit || this.AllowDeletes != oldDelete)
            {
                dataNav.OnChangePolicyModified(new ChangePolicyEventArgs(this.AllowAdditions, this.AllowEdits,
                                                                         this.AllowDeletes));
            }
            else
            {
                // Enable or disable Add and Delete based on the policy
                dataNav.btnAdd.Enabled    = (this.AllowAdditions && listManager != null);
                dataNav.btnDelete.Enabled = (this.AllowDeletes && listManager != null && listManager.Count > 0);
            }
        }