• dev的grid封装组件,拖拽初始化属性


     [ToolboxItem(true)]
        public partial class UserGridControl : GridControl
        {
            public new BaseView MainView
            {
                get
                {
                    return base.MainView;
                }
                set
                {
                    var gridView = value as GridView;
                    if (gridView == null) return;
                    gridView.TopRowChanged += OnTopRowChanged;
                    gridView.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator;
                    gridView.CustomDrawEmptyForeground += gridView1_CustomDrawEmptyForeground;
                    base.MainView = gridView;
                }
            }
    
            public override BaseView CreateView(string name)
            {
                var baseView = base.CreateView(name);
                if (!(baseView is GridView)) return baseView;
                var gridView = baseView as GridView;
                InitView(gridView);
                return gridView;
            }
    
            public void InitView(GridView gridView)
            {
                gridView.Appearance.FocusedRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
                gridView.Appearance.FocusedRow.Options.UseBackColor = true;
                gridView.Appearance.HideSelectionRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
                gridView.Appearance.HideSelectionRow.Options.UseBackColor = true;
                gridView.Appearance.SelectedRow.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(128)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
                gridView.Appearance.SelectedRow.Options.UseBackColor = true;
    
                gridView.OptionsBehavior.ReadOnly = true;
                gridView.OptionsBehavior.EditorShowMode = DevExpress.Utils.EditorShowMode.MouseDownFocused;
                gridView.OptionsDetail.EnableMasterViewMode = false;
                gridView.OptionsSelection.MultiSelectMode = GridMultiSelectMode.RowSelect;
                gridView.OptionsSelection.EnableAppearanceFocusedCell = false;
                //gridView.OptionsSelection.MultiSelect = true;
                gridView.OptionsView.ShowAutoFilterRow = true;
                gridView.OptionsView.ShowGroupPanel = false;
                gridView.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
                gridView.OptionsPrint.AutoWidth = false;
                gridView.IndicatorWidth = 30;
                gridView.TopRowChanged += OnTopRowChanged;
                gridView.CustomDrawRowIndicator += gridView_CustomDrawRowIndicator;
                gridView.CustomDrawEmptyForeground += gridView1_CustomDrawEmptyForeground;
            }
    
            public void BestColunm(GridView gridView, bool bestColumn)
            {
                gridView.OptionsView.ColumnAutoWidth = !bestColumn;
                gridView.BestFitColumns();
            }
    
            private void gridView_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
            {
                var gridView = (GridView)sender;
                if (!e.Info.IsRowIndicator || e.RowHandle < 0) return;
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
                e.Info.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
            }
    
            private void OnTopRowChanged(object sender, EventArgs e)
            {
                GridView view = sender as GridView;
                if (view == null)
                    return;
                int width = CalcIndicatorBestWidth(view);
                if ((view.IndicatorWidth - 4 < width || view.IndicatorWidth + 4 > width) && view.IndicatorWidth != width)
                {
                    if (width < 30)
                        width = 30;
                    view.IndicatorWidth = width;
                }
            }
    
            /// <summary>
            /// 计算行头宽度
            /// </summary>
            /// <param name="sender"></param>
            /// <returns></returns>
            private int CalcIndicatorBestWidth(DevExpress.XtraGrid.Views.Grid.GridView view)
            {
                Graphics graphics = new Control().CreateGraphics();
                SizeF sizeF = new SizeF();
                int count = view.TopRowIndex + ((DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)view.GetViewInfo()).RowsInfo.Count;
                if (count == 0)
                {
                    count = 30;
                }
                sizeF = graphics.MeasureString(count.ToString(), view.Appearance.Row.Font);
                return Convert.ToInt32(sizeF.Width) + 20;
            }
    
            private void gridView1_CustomDrawEmptyForeground(object sender, DevExpress.XtraGrid.Views.Base.CustomDrawEventArgs e)
            {
                if ((sender as GridView).RowCount == 0)
                {
                    int width = 300;
                    int height = 60;
                    int font_size = 18;
                    string str = "* 没有查询到数据!
    * no data found!";
                    Font f = new Font("微软雅黑", font_size, FontStyle.Bold);
                    Rectangle r = new Rectangle((e.Bounds.Width - width) / 2, (e.Bounds.Height - height) / 2, width, height);
                    e.Graphics.DrawString(str, f, Brushes.LightBlue, r);
                    e.Handled = true;
                }
            }
        }
  • 相关阅读:
    Ural 1966 Cycling Roads
    SQL Server 2008 安装(lpt亲测)
    cf Round#273 Div.2
    poj 2318 TOYS
    计算几何好模板
    ❤Friends
    限制pyqt5应用程序 只允许打开一次
    pyqt5 菜单栏+信息提示框
    Android Linux deploy
    system分区解锁
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/14103778.html
Copyright © 2020-2023  润新知