Пример #1
0
        void view_Load(object sender, EventArgs e)
        {
            BaseView bv = sender as BaseView;

            if (bv == null || bv.dgView.Columns.Count < 1)
            {
                return;
            }

            string      viewName = sender.GetType().Name;
            CfgViewData viewData = null;

            foreach (CfgViewData v in curUser.Cfg.Views)
            {
                if (v.Name == viewName)
                {
                    viewData = v;
                    break;
                }
            }
            if (viewData == null || viewData.Columns == null || viewData.Columns.Count < 1)
            {
                return;
            }
            // restore column layout
            foreach (DataGridViewColumn col in bv.dgView.Columns)
            {
                foreach (CfgColumnData ini in viewData.Columns)
                {
                    if (col.DataPropertyName == ini.DataPropertyName)
                    {
                        if (col.DisplayIndex >= 0 && col.DisplayIndex < bv.dgView.Columns.Count)
                        {
                            col.DisplayIndex = ini.DisplayIndex;
                            col.Width        = (ini.Width < 0 || ini.Width > 1000)?100:ini.Width;
                        }
                        break;
                    }
                }
            }
            // restore sorting
            if (viewData.SortColIdx >= 0 && viewData.SortColIdx < bv.dgView.Columns.Count)
            {
                DataGridViewColumn             newCol   = bv.dgView.Columns[viewData.SortColIdx];
                System.Windows.Forms.SortOrder newOrder = viewData.SortAsc?System.Windows.Forms.SortOrder.Ascending:System.Windows.Forms.SortOrder.Descending;
                if (bv.dgView.SortedColumn != newCol || bv.dgView.SortOrder != newOrder)
                {
                    if (bv.dgView.SortedColumn != null && bv.dgView.SortedColumn != newCol)
                    {
                        bv.dgView.SortedColumn.HeaderCell.SortGlyphDirection = System.Windows.Forms.SortOrder.None;
                    }

                    bv.dgView.Sort(newCol, viewData.SortAsc?ListSortDirection.Descending:ListSortDirection.Descending);
                    bv.dgView.SortedColumn.HeaderCell.SortGlyphDirection = newOrder;
                }
            }
        }
Пример #2
0
        void view_Destroyed(object sender, EventArgs e)
        {
            BaseView bv = sender as BaseView;

            if (bv == null || bv.dgView.Columns.Count < 1)
            {
                return;
            }

            try
            {
                string viewName = sender.GetType().Name;

                CfgViewData viewData = null;
                foreach (CfgViewData v in curUser.Cfg.Views)
                {
                    if (v.Name == viewName)
                    {
                        viewData = v;
                        break;
                    }
                }
                if (viewData == null)
                {
                    viewData      = new CfgViewData();
                    viewData.Name = viewName;
                    curUser.Cfg.Views.Add(viewData);
                }
                if (bv.dgView.SortedColumn != null)
                {
                    viewData.SortColIdx = bv.dgView.Columns.IndexOf(bv.dgView.SortedColumn);
                    viewData.SortAsc    = (bv.dgView.SortOrder == System.Windows.Forms.SortOrder.Ascending);
                }
                viewData.Columns.Clear();
                foreach (DataGridViewColumn col in bv.dgView.Columns)
                {
                    viewData.Columns.Add(new CfgColumnData(col.DisplayIndex, col.Width, col.DataPropertyName));
                }

                // save to database
                DB.ExecuteNoQuery("UPDATE Users SET user_cfgXML=@cfg WHERE user_id=" + curUser.ID,
                                  "@cfg", curUser.Cfg.SaveXML());
            }
            catch (Exception ex)
            {
#if DEBUG
                GM.ShowErrorMessageBox(this, "Saving view layout!", ex);
#endif
            }
        }