• 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;
                }
            }
        }
  • 相关阅读:
    常见HTTP状态(304,200等)
    ymPrompt消息提示组件 2.0,4.0
    将类型生成为模块
    about ValueType
    利用接口来改变已装箱值类型中的字段
    virtual\interface\abstract Class
    Response.Write("alert('hi')");显示在页面上
    The type eclipse.core.runtime.Plugin cannot be resolved.的解决
    Ubuntu11.04地址栏调整为文字模式
    Java的局部内部类以及final类型的参数和变量
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/14103778.html
Copyright © 2020-2023  润新知