public void InitializeControl(List<Column> dimensions, Object db)
        {
            _db = (DBManager)db;
            InitializeFields(dimensions);
            //SetFieldListSize(Size.Empty, new Size(250, 600));

            CellArgs = new CellInputDataEventArgs();

            #region Initialize Event Handlers

            FieldAreaChanging += PivotGridControlModifiedFieldAreaChanging;
            FieldFilterChanged += PivotGridControlModifiedFieldFilterChanged;
            CustomSummary += PivotGridControlRowOrientedCustomSummary;
            CellClick += PivotGridControlRowOrientedCellClick;

            #endregion
        }
        private void PivotGridControlRowOrientedCellClick(object sender, PivotCellEventArgs e)
        {
            if (e.RowField == null) return;
            
            #region Initialize Condition

            var fc = e.GetColumnFields();
            var fr = e.GetRowFields();

            var fieldValues = fc.Select(pivotGridField => new FieldValue { Field = pivotGridField.FieldName, Value = e.GetFieldValue(pivotGridField) }).ToList();
            fieldValues.AddRange(fr.Select(pivotGridField => new FieldValue { Field = pivotGridField.FieldName, Value = e.GetFieldValue(pivotGridField) }));

            CellArgs = new CellInputDataEventArgs();
            CellArgs.ConditionValues = fieldValues;

            #endregion
            
            #region Initialize Filters

            //var fields = GetFieldsByArea(FieldArea.FilterArea);
            //var filters = new List<FilterValues>();

            //foreach (var field in fields)
            //{
            //    filters.Add(new FilterValues { Field = field.FieldName, Values = field.FilterValues.ValuesIncluded.ToList(), ShowBlanks = field.FilterValues.ShowBlanks });
            //}

            var filters = (from field in Fields where field.Visible && field.FilterValues.HasFilter select new FilterValues { Field = field.FieldName, Values = field.FilterValues.ValuesIncluded.ToList(), ShowBlanks = field.FilterValues.ShowBlanks }).ToList();
            
            CellArgs.Filters = filters;

            #endregion

            #region Calculate Condition Locs
/*
            var setIL = _db.DataTableGetILByCondition(Table.TableRowSource, CellArgs.ConditionValues,
                                              CellArgs.Filters);
            var locs = _db.DataTableGetLocsByIL(setIL);
*/
            var userWhList = _db.UserWhList.Keys.ToList();
/*
            foreach (var loc in locs)
            {
                if (userWhList.Contains(loc))
                {
                    userWhList.Remove(loc);
                }
            }

            var items = _db.DataTableGetItemsByIL(setIL);

            foreach (var item in items)
            {
                var whs = _db.DataTableGetWhRowsByItem(item);
                foreach (var wh in whs)
                {
                    if (userWhList.Contains(wh))userWhList.Remove(wh);
                }
            }
*/
            #endregion

            #region Input Data Types

            //CellArgs.FieldName = e.DataField.FieldName;
            //CellArgs.DataTypeName = e.DataField.DataType.Name;))

            if (e.DataField == Fields["DIM_ITEMLOC_SUPPLIER_NEW"] || e.DataField == Fields["DIM_ITEMLOC_SUPPLIER_DESC_NEW"])
            {
                CellArgs.SetValues.Add(new FieldValue { Field = "DIM_ITEMLOC_SUPPLIER_NEW", Value = null });
                CellArgs.SetValues.Add(new FieldValue { Field = "DIM_ITEMLOC_SUPPLIER_DESC_NEW", Value = null });
                CellArgs.Type = InputDataTypes.Supplier;
                CellClickInputData(this, CellArgs);
            }
            else if (e.DataField == Fields["DIM_ITEMLOC_ORDERPLACE_NEW"])
            {
                CellArgs.SetValues.Add(new FieldValue { Field = e.DataField.FieldName, Value = null });
                CellArgs.Type = InputDataTypes.OrderPlace;

                #region Context Menu
                var menu = new ContextMenu();
                for (var i = OrderPlaces.Supplier; i < OrderPlaces.Office+1; i++)
                {
                    var menuItem = new MenuItem { Tag = (int)i, Header = i.Description() };
                    menuItem.Click += ContextMenuItemClick;
                    menu.Items.Add(menuItem);
                }

                //var menuItemOrderPlace1 = new MenuItem { Tag = (int)OrderPlace.Supplier, Header = OrderPlace.Supplier.Description() };
                //menuItemOrderPlace1.Click += ContextMenuItemOrderPlaceClick;

                //var menuItemOrderPlace2 = new MenuItem { Tag = (int)OrderPlace.Store, Header = OrderPlace.Store.Description() };
                //menuItemOrderPlace2.Click += ContextMenuItemOrderPlaceClick;

                //var menuItemOrderPlace3 = new MenuItem { Tag = (int)OrderPlace.Office, Header = OrderPlace.Office.Description() };
                //menuItemOrderPlace3.Click += ContextMenuItemOrderPlaceClick;

                //var menu = new ContextMenu();
                //menu.Items.Add(menuItemOrderPlace1);
                //menu.Items.Add(menuItemOrderPlace2);
                //menu.Items.Add(menuItemOrderPlace3);
                menu.IsOpen = true;

                #endregion
            }
            else if (e.DataField == Fields["DIM_ITEMLOC_SOURCEMETHOD_NEW"])
            {
                CellArgs.SetValues.Add(new FieldValue { Field = e.DataField.FieldName, Value = null });
                CellArgs.SetValues.Add(new FieldValue { Field = "DIM_ITEMLOC_SOURCEWH_NEW", Value = null });
                CellArgs.Type = InputDataTypes.SourceMethod;

                #region Context Menu
                var menuItemSourceMethod = new MenuItem { Tag = (char)SourceMethods.S, Header = SourceMethods.S.Description() };
                menuItemSourceMethod.Click += ContextMenuItemClick;
                var menu = new ContextMenu();
                menu.Items.Add(menuItemSourceMethod);

                //if (_db.UserWhList.Count != 0)
                if (userWhList.Count != 0)
                {
                    var menuItemSourceMethod2 = new MenuItem { Tag = (char)SourceMethods.W, Header = SourceMethods.W.Description() };
                    menuItemSourceMethod2.Click += ContextMenuItemClick;

                    var menuItemSourceMethod3 = new MenuItem { Tag = (char)SourceMethods.T, Header = SourceMethods.T.Description() };
                    menuItemSourceMethod3.Click += ContextMenuItemClick;
                    
                    menu.Items.Add(menuItemSourceMethod2);
                    menu.Items.Add(menuItemSourceMethod3);
                    menu.IsOpen = true;
                }

                menu.IsOpen = true;
                #endregion
            }
            else if (e.DataField == Fields["DIM_ITEMLOC_SOURCEWH_NEW"])
            {
                #region Check Source Method

                var r = e.GetRowFields();
                var sourcemethod = e.GetCellValue(null, r.Select(f => e.GetFieldValue(f)).ToArray(), Fields["DIM_ITEMLOC_SOURCEMETHOD_NEW"]) as CellRowOriented;
                if (sourcemethod == null) return;
                if (sourcemethod.Value == "" || 
                    Convert.ToChar(sourcemethod.Value) == (char)SourceMethods.S ||
                    sourcemethod.Value == "?") return;

                #endregion

                CellArgs.SetValues.Add(new FieldValue { Field = e.DataField.FieldName, Value = null});
                
                CellArgs.Type = InputDataTypes.SourceWh;

                #region Context Menu
                
                var menu = new ContextMenu();
                foreach (var wh in userWhList)
                {
                    //var menuItemSource = new MenuItem { Header = wh.Key, Tag = wh.Key };
                    var menuItemSource = new MenuItem { Header = wh, Tag = wh };
                    menuItemSource.Click += ContextMenuItemClick;
                    menu.Items.Add(menuItemSource);
                }
                menu.IsOpen = true;

                #endregion
            }
            else
            {
                return;
            }

            #endregion
        }
        private void PivotGridControl2CellClickInputData(object sender, CellInputDataEventArgs e)
        {
            if (e.Type == InputDataTypes.Supplier)
            {
                _args = e;
                var windowSupplier = new WindowSupplier(_db);
                windowSupplier.SupplierSelected += WindowSupplierSupplierSelected;
                ((UIElement)Content).IsEnabled = false;
                Cursor = Cursors.Wait;
                windowSupplier.ShowDialog();
                ((UIElement)Content).IsEnabled = true;
                Cursor = Cursors.Arrow;
            }
            else
            {
                var setIL = _db.DataTableGetILByCondition(Table.TableRowSource, e.ConditionValues,
                                                             e.Filters);
                if (e.Type == InputDataTypes.SourceWh)
                {
                    var chainGroup = _db.DataTableGetChainForCondition(setIL, Convert.ToInt32(e.SetValues[0].Value));
                    if (chainGroup != null)
                    {
                        var windowChain = new WindowChain(_db, chainGroup, setIL,_db.DataTableGetSourceMethodByIL(setIL)==SourceMethods.T);
                        windowChain.Apply += WindowChainApply;
                        windowChain.ShowDialog();

                        //var windowChainNative = new WindowChainNative(_db, chainGroup);
                        //windowChainNative.ShowDialog();
                    }
                    return;
                }

                var lockedIL = new SortedSet<IL>();
                if (_db.DataTableRowSourceUpdateCustom(e.SetValues, setIL, ref lockedIL) == false)
                {
                    MessageBox.Show("Ошибка при обновлении источника: " + _db.Error, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                else
                {
                    if (lockedIL.Count > 0)
                        MessageBox.Show("Не удалось выполненить действие для следующих товаров-подразделений: " + string.Join(",", lockedIL));
                    /*
                    if (e.Type == InputDataTypes.SourceMethod && e.SetValues[0].Value.Equals("S"))
                    {
                        //_db.FillDataTableCustom(DbManagerDynamic.TableSecSource);
                        //_db.DataTableSecSourceUpdateCustomWithoutDb(e.SetValues, e.ConditionValues);
                        _db.LogisticChainClear();
                    }
                     */
                    _pivotGridControl2.ReloadData();
                }
            }
            return;
        }