C# Winform重写DataGridView 有兴趣可以留言交流

//[ToolboxBitmap(typeof(DataGrid))]
public partial class UCDataGridView : DataGridView
{
//鼠标移动进来改变颜色
private bool _CellColorOnchange = false;
private bool _PaintCell = true;
private bool _canRefresh = true;
private List _columns = new List();
//鼠标移动到行上时的颜色
private Color cell_color = Utilities.PublicResource.YkdBasisColors.LightBlueColor;//Color.FromArgb(209, 244, 244);
//是否画三解
private bool shifouhuasanjiao = false;
private Color color_grid = Color.FromArgb(236, 233, 216);
#region
#endregion

    bool click = false;
    public UCDataGridView()
    {
        //this.DefaultCellStyle.SelectionBackColor = Color.FromArgb(209, 244, 244);
        this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        UpdateStyles();
    }
    protected override void OnCreateControl()
    {

        this.EnableHeadersVisualStyles = false;
        //this.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216);
        this.ColumnHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;
        //列头背景色,字体加粗
        this.EnableHeadersVisualStyles = false;
        this.ColumnHeadersDefaultCellStyle.BackColor = Utilities.PublicResource.YkdBasisColors.GrayBackColorF7;
        this.ColumnHeadersDefaultCellStyle.Font = MIP.Utilities.PublicResource.YkdTextFonts.TextFontBold;

        //this.ColumnHeadersHeight = 20;
        this.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
        this.ColumnHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
        this.ColumnHeadersDefaultCellStyle.ForeColor = Color.Black;
        //this.ColumnHeadersDefaultCellStyle.SelectionBackColor = System.Drawing.SystemColors.Highlight;
        //this.ColumnHeadersDefaultCellStyle.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
        //this.RowHeadersDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleLeft;
        this.RowHeadersDefaultCellStyle.BackColor = Color.FromArgb(236, 233, 216);
        this.RowHeadersDefaultCellStyle.ForeColor = System.Drawing.SystemColors.WindowText;
        this.RowHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
        //this.DefaultCellStyle.SelectionBackColor = Color.DarkBlue;
        //this.DefaultCellStyle.SelectionForeColor = Color.DarkSlateBlue;
        this.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;
        //this.GridColor = Color.Silver;//表格点击后颜色 表格线颜色
        this.BackgroundColor = System.Drawing.SystemColors.Window;
        this.BorderStyle = System.Windows.Forms.BorderStyle.None;
        this.AllowUserToOrderColumns = true;
        this.AutoGenerateColumns = true;
        base.OnCreateControl();
    }
    public int RowIndex = -1;

    public int CurrentRowIndexForMa = 0;
    Color defaultcolor;
    //移到单元格时的颜色
    protected override void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
    {
        try
        {
            if (e.RowIndex < 0)
                return;
            if (_CellColorOnchange)
            {
                if (e.RowIndex == RowIndex)
                {
                    Rows[e.RowIndex].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#006581");
                }
                else
                    Rows[e.RowIndex].DefaultCellStyle.BackColor = cell_color;
            }
            //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "OnCellMouseMove" + Rows[e.RowIndex].DefaultCellStyle.BackColor);
            //System.Threading.Thread.Sleep(100);
            foreach (UCDataGridViewImageAndDataModel item in this.IconColumnList)
            {
                //在这里可以根据数据判断ICON图标是否显示
                if (this.Columns[e.ColumnIndex].Name == item.ColumnName)
                {
                    if (e.RowIndex < 0)
                        return;

                    //数据与图片列
                    UCDataGridViewImageAndDataCell imageAndDataCell = this.Rows[e.RowIndex].Cells[e.ColumnIndex] as UCDataGridViewImageAndDataCell;
                    if (imageAndDataCell != null)   //判断当前是不是按钮列
                    {
                        if (imageAndDataCell.IsOnIconImage(null, e))
                        {
                            this.OnIconImageName = imageAndDataCell.OnIconImageName;
                        }
                    }
                }
            }

        }
        catch (Exception ex)
        {
            //throw ex;
        }
        base.OnCellMouseMove(e);
    }
    //进入单元格时保存当前的颜色
    protected override void OnCellMouseEnter(DataGridViewCellEventArgs e)
    {
        base.OnCellMouseEnter(e);
        if (e.RowIndex < 0)
            return;
        try
        {
            if (_CellColorOnchange)
                if (e.RowIndex == RowIndex)
                {
                    this.Rows[e.RowIndex].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#006581");
                }
                else
                    defaultcolor = Rows[e.RowIndex].DefaultCellStyle.BackColor;


        }
        catch (Exception ex)
        {
            throw ex;
        }
        //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "OnCellMouseEnter" + Rows[e.RowIndex].DefaultCellStyle.BackColor);
        //System.Threading.Thread.Sleep(100);

    }
    //离开时还原颜色
    protected override void OnCellMouseLeave(DataGridViewCellEventArgs e)
    {
        base.OnCellMouseLeave(e);
        try
        {
            if (e.RowIndex < 0)
                return;
            if (_CellColorOnchange)
            {
                //Rows[e.RowIndex].DefaultCellStyle.ForeColor = this.RowsDefaultCellStyle.ForeColor;
                for (int i = 0; i < Rows.Count; i++)
                {
                    if (i != e.RowIndex)
                    {

                    }


                    if (i == RowIndex)
                    {
                        Rows[i].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#006581");
                    }
                    else if (i == e.RowIndex)
                    {
                        Rows[i].DefaultCellStyle.BackColor = defaultcolor;
                    }
                    else if (i % 2 == 1)
                    {
                        Rows[i].DefaultCellStyle.BackColor = this.AlternatingRowsDefaultCellStyle.BackColor;
                    }
                    else
                    {
                        Rows[i].DefaultCellStyle.BackColor = this.RowsDefaultCellStyle.BackColor;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
        //Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff") + "OnCellMouseLeave" + Rows[e.RowIndex].DefaultCellStyle.BackColor);
        //System.Threading.Thread.Sleep(100);

    }



    /// <summary>
    /// 自定义列
    /// </summary>
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<DataGridViewColumn> CUColumns
    {
        get { return _columns; }
        set
        {
            _columns = value;
            LoadColumns();
        }
    }
    private List<UCDataGridViewColumn> _orderColumns = new List<UCDataGridViewColumn>();
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<UCDataGridViewColumn> OrderColumns
    {
        get { return _orderColumns; }
        set { _orderColumns = value; }
    }

    /// <summary>
    /// 加载列
    /// </summary>
    private void LoadColumns()
    {
        try
        {
            this.Columns.Clear();
            if (OrderColumns.Count > 0 && OrderColumns.Count == _columns.Count)
            {
                foreach (UCDataGridViewColumn _orderItem in OrderColumns.OrderBy(x => x.OrderSort))
                {
                    foreach (DataGridViewColumn item in _columns)
                    {
                        if (item.Name == _orderItem.Column.Name)
                        {
                            this.Columns.Add(item);
                            break;
                        }

                    }
                }
            }
            else
            {
                foreach (DataGridViewColumn item in _columns)
                {

                    this.Columns.Add(item);
                }
            }
        }
        finally
        {
        }
    }
    /// <summary>
    /// 鼠标移动进来改变颜色 外部暴露属性
    /// </summary>
    [Description("获取或设置 鼠标移动进来改变颜色"), Category("自定义")]
    public bool CellColorOnchange
    {
        get
        {
            return _CellColorOnchange;
        }
        set
        {
            _CellColorOnchange = value;
        }
    }
    /// <summary>
    /// 默认颜色设置
    /// </summary>
    public Color DefaultcolorSet
    {
        get
        {
            return cell_color;
        }
        set
        {
            cell_color = value;
        }
    }
    public bool Shifouhua_Sanjiao
    {
        get
        {
            return shifouhuasanjiao;
        }
        set
        {
            shifouhuasanjiao = value;
        }
    }

    #region 各列所需属性
    private List<UCDataGridViewActionButtonModel> _buttons = new List<UCDataGridViewActionButtonModel>();
    /// <summary>
    /// 图片按钮
    /// </summary>
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<UCDataGridViewActionButtonModel> ButtonsList
    {
        get { return _buttons; }
        set
        {
            _buttons = value;
            //loadButton();
        }
    }

    public int ClickButtonIndex = -1;

    public string OnIconImageName { get; set; }

    private List<UCDataGridViewImageAndDataModel> _iconImageList = new List<UCDataGridViewImageAndDataModel>();
    /// <summary>
    /// icon图片List
    /// </summary>
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<UCDataGridViewImageAndDataModel> IconColumnList { get { return _iconImageList; } set { _iconImageList = value; } }


    //private List<Color> _fontColr = new List<Color>();
    //[Bindable(false), Browsable(false)]
    //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    //public List<Color> FontColor { get { return _fontColr; } set { _fontColr = value; } }

    private List<DataGridViewColorBlockCellModel> _blockCellModelList = new List<DataGridViewColorBlockCellModel>();

    /// <summary>
    /// 模块列所需用值
    /// </summary>
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<DataGridViewColorBlockCellModel> BlockCellModelList
    {
        get { return _blockCellModelList; }
        set { _blockCellModelList = value; }
    }



    #endregion
    public Color Content_Grid_color
    {
        get
        {
            return color_grid;
        }
        set
        {
            color_grid = value;
        }
    }
    public bool CanRefresh
    {
        get { return _canRefresh; }
        set { _canRefresh = value; }
    }
    //是否重绘单元格
    public bool PaintCell
    {
        get { return _PaintCell; }
        set { _PaintCell = value; }
    }

    private void InitializeComponent()
    {
        ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
        this.SuspendLayout();
        this.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
        ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
        this.ResumeLayout(false);
    }
    //RowPostPaint
    protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
    {

        if (shifouhuasanjiao)
        {
            using (SolidBrush b = new SolidBrush(Color.Black))
            {
                //Image image = global::CRD.WinUI.Properties.Resources.未标题_1;
                //e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4);
                //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13);
                if (click)
                    if (e.RowIndex == this.CurrentRow.Index)
                    {
                        e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4);
                        //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13);
                    }

            }
        }


        base.OnRowPostPaint(e);
    }


    protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
    {
        if (shifouhuasanjiao)
        {
            using (SolidBrush b = new SolidBrush(Color.Black))
            {
                //Image image = global::CRD.WinUI.Properties.Resources.未标题_1;
                //e.Graphics.DrawString("►", e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 5, e.RowBounds.Location.Y + 4);
                //e.Graphics.DrawImageUnscaled(image, e.RowBounds.Location.X + 1, e.RowBounds.Location.Y + 2, 8, 13);
            }
        }
        base.OnRowPrePaint(e);
    }
    /// <summary>
    /// 重写单元格点击
    /// </summary>
    /// <param name="e"></param>
    protected override void OnCellClick(DataGridViewCellEventArgs e)
    {

        if (e.RowIndex > -1 && this.CurrentRow.Index == e.RowIndex)
        {
            click = true;
        }
        if (e.RowIndex < 0)
            return;
        //UCDataGridViewActionButtonCell cell = this.Rows[e.RowIndex].Cells[e.ColumnIndex] as UCDataGridViewActionButtonCell;
        //if (cell != null)
        //{
        //    if (!IsButtonClick(null, e))
        //        ClickButtonIndex = -1;
        //}

        DataGridViewColorBlockCell blockCell = this.Rows[e.RowIndex].Cells[e.ColumnIndex] as DataGridViewColorBlockCell;
        if (blockCell == null)
        {
            this.ClearSelection();
        }


        //Rectangle cellBounds = this[e.ColumnIndex, e.RowIndex].ContentBounds;
        //Brush brushCellBack = new SolidBrush(this.Rows[e.RowIndex].DefaultCellStyle.BackColor);
        //Graphics graphics = this.CreateGraphics();
        //graphics.FillRectangle(brushCellBack,
        //    cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height);
        base.OnCellClick(e);//最后,不然直接执行Form的点击事件了
    }



    protected override void OnSelectionChanged(EventArgs e)
    {
        base.OnSelectionChanged(e);
        //this.ClearSelection();
    }

    /// <summary>
    /// 重画单元格
    /// </summary>
    /// <param name="e"></param>
    protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
    {
        base.OnCellPainting(e);
        return;
        if (!PaintCell) return;
        try
        {
            base.OnCellPainting(e);
            if (e.RowIndex == -1)
            {
                SolidBrush b = new SolidBrush(Color.FromArgb(236, 233, 216));
                Pen whitePen = new Pen(color_grid, 0);
                int X = e.CellBounds.X;
                int Y = e.CellBounds.Y;
                int W = e.CellBounds.Width;
                int H = e.CellBounds.Height;

                Pen blackPen = new Pen(Color.FromArgb(242, 242, 242));//边框颜色 与背景图一致Color.FromArgb(242, 242, 242)

                //Image image = Image.FromFile(@"Resources\bg.jpg");// Properties.Resources._title1;//背景图片
                //TextureBrush tBrush = new TextureBrush(image);

                e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(242, 242, 242)), new Rectangle(X, Y, W, H));
                SolidBrush backColorBrush = new SolidBrush(e.CellStyle.BackColor);//e.CellStyle.BackColor
                e.Graphics.DrawRectangle(blackPen, e.CellBounds);
                //DataGridViewColorBlockCell blockcell = this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex] as DataGridViewColorBlockCell;
                //if (blockcell != null)
                //{
                //    using (Brush drawBrush = new SolidBrush(Color.White))
                //    {
                //        using (Pen gridLinePen = new Pen(drawBrush, 2))
                //        {
                //            //e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                //            //SolidBrush bush = new SolidBrush(Color.FromArgb(1, 185, 209));//填充的颜色
                //            e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(1, 185, 209)), e.CellBounds.X + e.CellBounds.Width - 50, e.CellBounds.Y + 10, 20, 20);
                //            Font _font = new Font("微软雅黑", 8);
                //            e.Graphics.DrawString("100", _font, drawBrush, e.CellBounds.Right - 52, e.CellBounds.Top + 12);
                //        }
                //    }
                //}


                foreach (DataGridViewColorBlockCellModel item in BlockCellModelList)
                {
                    if (this.Columns[e.ColumnIndex].Name == item.ColumnName && this.Rows.Count > 0)
                    {
                        //数据与图片列
                        DataGridViewColorBlockCell blockcell = this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex] as DataGridViewColorBlockCell;
                        if (blockcell != null)
                        {
                            try
                            {
                                using (Brush drawBrush = new SolidBrush(item.HeaderFontColor))
                                {
                                    using (Pen gridLinePen = new Pen(drawBrush, 2))
                                    {
                                        //Graphics graphics = this.CreateGraphics();
                                        //指定下面一句,可以消除锯齿。但是会使表格有网格线。
                                        //实际应用中。圆的位置要根据具体情况再定位。不然会有偏移  
                                        if (item.ShowCircular)
                                        {
                                            if (!string.IsNullOrEmpty(item.HeaderFontText))
                                            {
                                                Font _font = new Font("微软雅黑", 10, System.Drawing.FontStyle.Bold);
                                                e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
                                                SizeF sizeText = e.Graphics.MeasureString(this.Columns[e.ColumnIndex].HeaderText, this.ColumnHeadersDefaultCellStyle.Font);
                                                if (item.CanSowCircular)
                                                    e.Graphics.FillEllipse(new SolidBrush(Color.FromArgb(1, 185, 209)), e.CellBounds.X + (e.CellBounds.Width - (int)Math.Round(sizeText.Width)) / 2 + (int)Math.Round(sizeText.Width) + 5, e.CellBounds.Y + 10, 22, 22);

                                                e.Graphics.DrawString(item.HeaderFontText, _font, drawBrush, e.CellBounds.X + (e.CellBounds.Width - (int)Math.Round(sizeText.Width)) / 2 + (int)Math.Round(sizeText.Width) + 6, e.CellBounds.Top + 11);
                                            }
                                        }
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                //throw ex;
                            }
                        }
                    }
                }

                e.PaintContent(e.CellBounds);
                e.Handled = true;
                //if (e.ColumnIndex == 0)
                //    e.Graphics.DrawRectangle(blackPen, new Rectangle(X, Y + 1, W - 1, H - 2));
                //else
                //    e.Graphics.DrawRectangle(blackPen, new Rectangle(X - 1, Y + 1, W, H - 2));
                //try
                //{
                //    if (e.ColumnIndex > 0)
                //    {
                //        DataGridViewColorBlockCell cell = this.Rows[e.RowIndex + 1].Cells[e.ColumnIndex] as DataGridViewColorBlockCell;
                //        if (cell != null)
                //        {
                //            try
                //            {
                //                using (Brush gridBrush = new SolidBrush(Color.Green), drawBrush = new SolidBrush(Color.Yellow))
                //                {
                //                    using (Pen gridLinePen = new Pen(gridBrush, 2))
                //                    {
                //                        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
                //                        Brush bush = new SolidBrush(Color.Green);//填充的颜色
                //                        e.Graphics.FillEllipse(bush, e.CellBounds.X + e.CellBounds.Width - 50, e.CellBounds.Y + 6, 25, 25);
                //                        Font _font = new Font("微软雅黑", 10);
                //                        e.Graphics.DrawString("100", _font, drawBrush, e.CellBounds.Right - 52, e.CellBounds.Top + 8);
                //                    }
                //                }
                //            }
                //            catch (Exception ex)
                //            {
                //                throw ex;
                //            }
                //        }


                //    }
                //}
                //catch (Exception ex)
                //{

                //}

            }
            //else {
            //    //Color.FromArgb(193, 193, 193)
            //    Rectangle border = e.CellBounds;
            //    border.Offset(new Point(0, 0));

            //    e.Graphics.DrawRectangle(whitePen, border);
            //}



            #region 备用
            //else if (e.RowIndex == -1)
            //{
            //    //标题行
            //    using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.Silver,
            //      Color.Silver, LinearGradientMode.Vertical))
            //    {
            //        e.Graphics.FillRectangle(b, e.CellBounds);
            //        Rectangle border = e.CellBounds;
            //        border.Offset(new Point(-1, -1));
            //        e.Graphics.DrawRectangle(Pens.Silver, border);
            //        //e.Graphics.DrawRectangle(Pens.Black, border.X + 1, border.Y + 1, border.Width - 1, border.Height - 1);
            //    }
            //    e.PaintContent(e.CellBounds);
            //    e.Handled = true;
            //}
            //else if (e.ColumnIndex == -1)
            //{
            //    //标题列
            //    using (LinearGradientBrush brush = new LinearGradientBrush(e.CellBounds, Color.Silver,
            //      Color.Silver, LinearGradientMode.Horizontal))
            //    {

            //        e.Graphics.FillRectangle(b, e.CellBounds);
            //        Rectangle border = e.CellBounds;
            //        border.Offset(new Point(-1, -1));
            //        e.Graphics.DrawRectangle(Pens.Silver, border);
            //        //e.Graphics.DrawRectangle(Pens.Black, border.X+1,border.Y+1,border.Width-1,border.Height-1);
            //        e.Graphics.DrawString("△", Font, b, e.CellBounds.X, e.CellBounds.Y);
            //    }
            //    e.PaintContent(e.CellBounds);
            //    e.Handled = true;
            //}
            //else
            //{
            //    //Color.FromArgb(193, 193, 193)
            //    Rectangle border = e.CellBounds;
            //    border.Offset(new Point(-1, -1));

            //    e.Graphics.DrawRectangle(whitePen, border);
            //}
            #endregion
        }
        catch (Exception ex)
        {
            Console.WriteLine("OnCellPainting");
            Console.WriteLine(ex.Message);
        }

    }

    /// <summary>
    /// 数据源绑定完成后
    /// </summary>
    /// <param name="e"></param>
    protected override void OnDataSourceChanged(EventArgs e)
    {
        RefreshGrid();
    }
    protected override void OnDataBindingComplete(DataGridViewBindingCompleteEventArgs e)
    {
        try
        {
            base.OnDataBindingComplete(e);
            if (OrderColumns.Count > 0 && OrderColumns.Count == this.Columns.Count)
            {
                foreach (UCDataGridViewColumn _orderItem in OrderColumns.OrderBy(x => x.OrderSort))
                {
                    foreach (DataGridViewColumn item in _columns)
                    {
                        if (item.Name == _orderItem.Column.Name)
                        {
                            item.DisplayIndex = _orderItem.OrderSort;
                            break;
                        }

                    }
                }
            }

            RefreshGrid();
            this.ClearSelection();
        }
        catch (Exception ex)
        {
            Console.WriteLine("OnDataBindingComplete");
            Console.WriteLine(ex.Message);
        }

    }

    public void RefreshGrid()
    {
        if (_canRefresh)
        {
            for (int i = 0; i < this.Rows.Count; i++)
            {
                OnCellMouseEnter(new DataGridViewCellEventArgs(0, i));
                OnCellMouseLeave(new DataGridViewCellEventArgs(0, i));
            }
        }
    }

    public void RefreshGridAllClomuns()
    {

        for (int i = 0; i < this.Rows.Count; i++)
        {
            for (int j = 0; j < this.Columns.Count; j++)
            {
                OnCellMouseEnter(new DataGridViewCellEventArgs(j, i));
                OnCellMouseLeave(new DataGridViewCellEventArgs(j, i));
            }
        }
    }


    protected override void OnSorted(EventArgs e)
    {
        RefreshGrid();
        base.OnSorted(e);
    }


    /// <summary>
    /// 判断用户是否单击了修改按钮,DataGridView发生CellMouseClick事件时,
    /// 因本单元格中有两个按钮,本方法通过坐标判断用户是否单击了修改按钮。
    /// </summary>
    public bool IsButtonClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        try
        {
            for (int i = 0; i < this.ButtonsList.Count; i++)
            {
                if (this.CurrentRow.Index < 0) return false;
                Rectangle cellBounds = this[e.ColumnIndex, e.RowIndex].ContentBounds;
                Rectangle paintCellBounds = this.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);

                paintCellBounds.Width = this.Columns[e.ColumnIndex].Width;
                paintCellBounds.Height = this.Rows[e.RowIndex].Height;
                int nowX = GetPositionX(ref cellBounds);
                if (i != 0)
                {
                    for (int j = 0; j < i; j++)
                    {
                        nowX = nowX + this.ButtonsList[j].ImageBmp.Width + 2;//计算下一个坐标点
                    }
                }
                Rectangle recModify = new Rectangle(nowX, cellBounds.Location.Y + (cellBounds.Height - this.ButtonsList[i].ImageBmp.Height) / 2, this.ButtonsList[i].ImageBmp.Width, this.ButtonsList[i].ImageBmp.Height);
                if (IsInRect(e.X, cellBounds.Location.Y + (cellBounds.Height - this.ButtonsList[i].ImageBmp.Height) / 2, recModify))
                {
                    ClickButtonIndex = i;
                    return true;
                }
            }
            return false;
        }
        catch (Exception ex)
        {
            return false;
            Console.WriteLine(ex.Message);
        }

    }
    /// <summary>
    /// 获取初始化X点坐标
    /// </summary>
    /// <param name="cellBounds"></param>
    /// <returns></returns>
    private int GetPositionX(ref Rectangle cellBounds)
    {
        int allButtonWidth = 0;

        for (int i = 0; i < this.ButtonsList.Count; i++)
        {
            allButtonWidth += this.ButtonsList[i].ImageBmp.Width;
        }
        allButtonWidth += (this.ButtonsList.Count - 1) * 2;  //间距
        int nowX = cellBounds.Location.X + (cellBounds.Width / 2 - allButtonWidth / 2) - 2;
        return nowX;
    }
    /// <summary>
    /// 判断鼠标坐标是否在指定的区域内。
    /// </summary>
    private bool IsInRect(int x, int y, Rectangle area)
    {
        if (x > area.Left && x < area.Right && y >= area.Top && y < area.Bottom)
            return true;
        return false;

    }

    /// <summary>
    /// 鼠标点击单元格的事件
    /// </summary>
    /// <param name="e"></param>
    protected override void OnCellMouseClick(DataGridViewCellMouseEventArgs e)
    {
        try
        {
            if (e.RowIndex < 0)
            {
                //全选CheckBox点击时RowIndex=-1必须执行base.Click
                base.OnCellMouseClick(e);
                return;
            }

            UCDataGridViewActionButtonCell cell = this.Rows[e.RowIndex].Cells[e.ColumnIndex] as UCDataGridViewActionButtonCell;
            if (cell != null)
            {
                if (!IsButtonClick(null, e))
                    ClickButtonIndex = -1;
            }
            base.OnCellMouseClick(e);

            this.ClickButtonIndex = -1;   //不赋值-1,会有问题 。
        }
        catch (Exception ex)
        {
            Console.WriteLine("OnCellMouseClick");
            Console.WriteLine(ex.Message);
        }


    }


    /// <summary>
    /// 单元格格式化方法
    /// </summary>
    /// <param name="e"></param>
    protected override void OnCellFormatting(DataGridViewCellFormattingEventArgs e)
    {
        #region 各特殊列
        try
        {

            base.OnCellFormatting(e);
            return;
        }
        catch (Exception ex)
        {
            Console.WriteLine("OnCellFormatting");
            Console.WriteLine(ex.Message);
        }

        #endregion
    }



}
//System.Windows.Forms.DataGridViewColumn
public class UCDataGridViewColumn : System.Windows.Forms.DataGridViewColumn
{
    private int _orderSort = 0;
    /// <summary>
    /// 排序字段
    /// </summary>
    public int OrderSort { get; set; }

    private DataGridViewColumn _columnName = new DataGridViewColumn();
    /// <summary>
    /// 字段名称
    /// </summary>
    public DataGridViewColumn Column
    {
        get; set;
    }
}

–工具类
public class DGVStyleHellper
{
public DGVStyleHellper()
{

    }
    private DataGridView _ucDataGridView =null;
    public DataGridView UCDGV
    { get
        {
            return _ucDataGridView;
        }
        set
        {
            _ucDataGridView = value;
        }
    }

    /// <summary>
    /// 图标样式
    /// </summary>
    public void SetDGVStyle ()
    {
        if (_ucDataGridView == null)
        {
            return;
        }
        else
        {
            _ucDataGridView.ColumnHeadersDefaultCellStyle.BackColor = Utilities.PublicResource.YkdBasisColors.GrayBackColorF7;
            _ucDataGridView.ColumnHeadersDefaultCellStyle.Font = MIP.Utilities.PublicResource.YkdTextFonts.TextFontBold;
            //标题
            _ucDataGridView.ColumnHeadersBorderStyle = System.Windows.Forms.DataGridViewHeaderBorderStyle.None;
            _ucDataGridView.ColumnHeadersHeight = 40;
            _ucDataGridView.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.None;

            //普通行
            _ucDataGridView.AutoGenerateColumns = false;//不允许自动生成列
            _ucDataGridView.RowTemplate.Height = 40;  //设置行高
            _ucDataGridView.RowHeadersVisible = false;
            _ucDataGridView.CellBorderStyle = System.Windows.Forms.DataGridViewCellBorderStyle.None;
            _ucDataGridView.BorderStyle = BorderStyle.None;

            //隔行变色
            _ucDataGridView.RowsDefaultCellStyle.BackColor = Color.FromArgb(255, 255, 255); //Color.Bisque;
            _ucDataGridView.AlternatingRowsDefaultCellStyle.BackColor = Color.FromArgb(242, 242, 242);

            //设置行改变颜色
            _ucDataGridView.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.CellSelect;

            _ucDataGridView.DefaultCellStyle.SelectionBackColor =Color.FromArgb(209,244,244);
            //_ucDataGridView.DefaultcolorSet = Color.FromArgb(209, 244, 244);

            _ucDataGridView.RowsDefaultCellStyle.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
            _ucDataGridView.RowsDefaultCellStyle.Font = new Font("微软雅黑", 10, FontStyle.Regular);

            //设置标题字体
            //_ucDataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("微软雅黑", 10, FontStyle.Bold);

            //_ucDataGridView.Dock = System.Windows.Forms.DockStyle.Fill;
            
            _ucDataGridView.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.Fill;

            //禁止自动添加行
            _ucDataGridView.AllowUserToAddRows = false;
            _ucDataGridView.AllowUserToResizeRows = false;
            _ucDataGridView.AllowUserToResizeColumns = false;


        }

    }



   
}

–背景块列
–第一次尝试封装成控件形式。画列。后来这种形式基本上不用了。项目里用的更简洁的方式。

public class DataGridViewColorBlockColumn : DataGridViewColumn
{
public DataGridViewColorBlockColumn() : base(new DataGridViewColorBlockCell())
{
CellTemplate = new DataGridViewColorBlockCell();
}
}
public class DataGridViewColorBlockCell : DataGridViewTextBoxCell
{

    private Color _headerBackColor = new Color();
    /// <summary>
    /// 表头圆圈背景色    
    /// </summary>
    public Color HeaderBackColor { get { return _headerBackColor; } set { _headerBackColor = value; } }

    private Color _headerFontColor = new Color();
    /// <summary>
    /// 表头字体颜色
    /// </summary>
    public Color HeaderFontColor { get { return _headerFontColor; } set { _headerFontColor = value; } }

    private string _headerFontText ="";
    /// <summary>
    /// 表头圆形内文字
    /// </summary>
    public string HeaderFontText { get { return _headerFontText; } set { _headerFontText = value; } }

    private int _buttonWidth = 0;
    /// <summary>
    /// 列中按钮的宽度
    /// </summary>
    public int ButtonWidth { get { return _buttonWidth; } set { _buttonWidth = value; } }


    private Color _blockBackColor = Color.FromArgb(28, 132, 198);
    /// <summary>
    /// 块 默认背景色
    /// </summary>
    public Color BlockBackColor { get { return _blockBackColor; } set { _blockBackColor = value; } }

    private Color _blockFontColor = Color.White;
    /// <summary>
    /// 块 默认前景色
    /// </summary>
    public Color BlockFontColor { get { return _blockFontColor; } set { _blockFontColor = value; } }

    private Color _blockNoFontColor = Color.Black;
    /// <summary>
    /// 不可用的块 前景色
    /// </summary>
    public Color BlockNoFontColor { get { return _blockNoFontColor; } set { _blockNoFontColor = value; } }

    private Color _blockClickFontColor = Color.White;
    /// <summary>
    /// 块点击时前景颜色
    /// </summary>
    public Color BlockClickFontColor { get { return _blockClickFontColor; } set { _blockClickFontColor = value; } }

    private bool _canShowCircular = true;
    public bool CanSowCircular
    {
        get { return _canShowCircular; }
        set { _canShowCircular = value; }
    }

    private Color _blockClickColor = Color.FromArgb(255, 128, 30);
    /// <summary>
    /// 块 点击时背景颜色
    /// </summary>
    public Color BlockClickColor { get { return _blockClickColor; } set { _blockClickColor = value; } }

    private List<Color> _fontColr = new List<Color>();
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<Color> FontColor { get { return _fontColr; } set { _fontColr = value; } }

    private bool _canChangeColor = true;
    /// <summary>
    /// 是否应用颜色  同时控制默认颜色和点击时颜色
    /// </summary>
    public bool CanChangeColor { get { return _canChangeColor; } set { _canChangeColor = value; } }


    private Font _commonfont;
    public Font CommonFont
    {
        get { return _commonfont; }
        set { _commonfont = value; }
    }


    protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
        object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle,
        DataGridViewPaintParts paintParts)
    {

        cellBounds = PrivatePaint(graphics, cellBounds, rowIndex, cellStyle, true, value, BlockBackColor);
        //base.PaintBorder(graphics, clipBounds, cellBounds, cellStyle, advancedBorderStyle);
        //nowColIndex = this.DataGridView.Columns.Count - 1;
    }

    /// <summary>
    /// 私有的单元格重绘方法,根据鼠标是否移动到按钮上,对按钮的不同背景和边框进行绘制。
    /// </summary>
    private Rectangle PrivatePaint(Graphics graphics, Rectangle cellBounds, int rowIndex, DataGridViewCellStyle cellStyle, bool clearBackground, object value, Color _color)
    {
        //Console.WriteLine("当前行:"+rowIndex);
        _color = this.Selected ? BlockClickColor : BlockBackColor;
        Brush brushCellBack = new SolidBrush(cellStyle.BackColor);
        graphics.FillRectangle(brushCellBack, cellBounds.X, cellBounds.Y, cellBounds.Width, cellBounds.Height);
        //}
        Font _font = YkdTextFonts.TextFont;
        if (value == null)
        {
            value = "";
        }
        value = value.ToString().Replace("\0\0", "\0 ");

       //if (rowIndex % 2 == 0)
       //    value = "99 0";

       SizeF sizeText = graphics.MeasureString(value.ToString(), _font);   //取字符串按字体时的大小
        //int nowX = cellBounds.Location.X + (cellBounds.Width / 2 - (int)sizeText.Width / 2) - 15;
       // int nowX = cellBounds.Location.X + 8;
        //int fontX = cellBounds.Location.X + ((cellBounds.Width/2 - (int)sizeText.Width/2))/2 -20;
        //int fontY = cellBounds.Location.Y + 8;
        int nButtonWidth = 0;
        if (_buttonWidth == 0)
        {
            nButtonWidth = cellBounds.Width-16;
        }
        else
        {
            nButtonWidth = _buttonWidth;
        }

        var tRect1 = new Rectangle(cellBounds.Location.X+ (cellBounds.Width - nButtonWidth) / 2+5, cellBounds.Location.Y+10, (nButtonWidth) / 2, cellBounds.Height);
        var tRect2 = new Rectangle(cellBounds.Location.X+ (cellBounds.Width) / 2-5, cellBounds.Location.Y+10, (nButtonWidth) / 2, cellBounds.Height);
        int nowX = cellBounds.Location.X + (cellBounds.Width - nButtonWidth) / 2;
        int fontX = cellBounds.Location.X + (cellBounds.Width - nButtonWidth) / 2 + (nButtonWidth - (int)sizeText.Width) / 2;
        if (value.ToString().Split(' ').Length > 2)
        {
            //value.ToString().Split(' ')[0]
            SizeF sizeTexti = graphics.MeasureString(value.ToString().Split(' ')[0], _font);
            if ((int)sizeTexti.Width < 15)
                fontX = (cellBounds.Location.X + (cellBounds.Width - nButtonWidth) / 2 + (nButtonWidth - (int)sizeText.Width - 10) / 2) + 0;
            else
                fontX = (cellBounds.Location.X + (cellBounds.Width - nButtonWidth) / 2 + (nButtonWidth - (int)sizeText.Width - 10) / 2)-8;
        }
        int fontY = cellBounds.Location.Y + 10;//cellBounds.Location.Y + ((cellBounds.Height)/ 2)/2+2  ;
        SolidBrush drawBrush = null;
        if (CanChangeColor)
        {
            Rectangle recButton = new Rectangle(nowX, cellBounds.Location.Y + 2, nButtonWidth, cellBounds.Height - 8);
            Pen p = new Pen(_color);
            graphics.FillRectangle(new SolidBrush(_color), recButton.X, recButton.Y+2, recButton.Width, recButton.Height);
            graphics.DrawRectangle(p, recButton.X, recButton.Y+2, recButton.Width, recButton.Height);

            if (this.Selected)
            {
                drawBrush = new SolidBrush(_blockClickFontColor);
            }
            else
            {
                drawBrush = new SolidBrush(_blockFontColor);
            }                

        }else
        {
            drawBrush = new SolidBrush(_blockNoFontColor);
        }
        if (!value.ToString().Contains(" "))  //不包含空格  一个值
        {
            
            if (CanChangeColor)
                graphics.DrawString(value.ToString(), _font, drawBrush, fontX, fontY);
            else if (CommonFont != null)
                graphics.DrawString(value.ToString(), CommonFont, drawBrush, fontX, fontY);
            else
                graphics.DrawString(value.ToString(), _font, drawBrush, fontX, fontY);
        }
        else   //包含空格
        {
            if (this.FontColor.Count > 0)   //赋值不为空
            {
                string[] values = value.ToString().Split(' ');
                int fontIndex = 0;
                for (int i = 0; i < values.Length; i++)
                {
                    if (fontIndex > this.FontColor.Count)
                    {
                        fontIndex = 0;
                    }
                    if (CanChangeColor)
                    {
                        if (this.Selected)
                        {
                            drawBrush = new SolidBrush(Color.White);
                        }
                        else
                        {
                            drawBrush = new SolidBrush(this.FontColor[fontIndex]);
                        }
                    }
                    else
                    {
                        drawBrush = new SolidBrush(Color.Black);
                    }
                    SizeF sizeTexti = graphics.MeasureString(values[i]+" ", _font);
                    if (value.ToString().Split(' ').Length <= 2)
                    {
                        if (i > 0)
                        {
                            graphics.DrawString(values[i] + " ", _font, drawBrush, tRect2, new StringFormat() { Alignment = StringAlignment.Center });
                        }
                        else
                        {
                            graphics.DrawString(values[i] + " ", _font, drawBrush, tRect1, new StringFormat() { Alignment = StringAlignment.Center });
                        }
                    }
                    else
                    {
                        if (i > 0)
                        {
                            fontX = fontX + (int)sizeTexti.Width + 20;
                        }
                        graphics.DrawString(values[i] + " ", _font, drawBrush, fontX, fontY, new StringFormat() { Alignment = StringAlignment.Center });
                    }
                        
                    //
                    fontIndex++;
                }
            }
            else   //为空,默认只取一个值
            {
                if(CanChangeColor)
                    graphics.DrawString(value.ToString(), _font, drawBrush, fontX, fontY);
                else if(CommonFont != null)
                    graphics.DrawString(value.ToString(), CommonFont, drawBrush, fontX, fontY);
                else
                    graphics.DrawString(value.ToString(), _font, drawBrush, fontX, fontY);
            }
            
        }
          //cellBounds.Location.Y + 10以行高为40计算
        return cellBounds;
    }

    protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)//DataGridViewCellMouseEventArgs
    {
        //Font _font = new Font("微软雅黑", 10);

        Graphics graphics = this.DataGridView.CreateGraphics();
        
        //int newrowIndex = this.DataGridView.CurrentRow.Index;
        //int newcolumnindex = this.DataGridView.CurrentCell.ColumnIndex;
        //BlockCellSingleton.Instance.newRowIndex = this.DataGridView.CurrentRow.Index;
        //BlockCellSingleton.Instance.newColumnIndex = this.DataGridView.CurrentCell.ColumnIndex;
        //Rectangle cellBounds = DataGridView[newcolumnindex, newrowIndex].ContentBounds;
        //Rectangle paintCellBounds = DataGridView.GetCellDisplayRectangle(newcolumnindex, newrowIndex, true);
        //paintCellBounds.Width = DataGridView.Columns[newcolumnindex].Width;
        //paintCellBounds.Height = DataGridView.Rows[newrowIndex].Height;
        //SizeF sizeText = graphics.MeasureString(DataGridView[newcolumnindex, newrowIndex].Value.ToString(), _font);   //取字符串按字体时的大小
        //int nowX = cellBounds.Location.X + (cellBounds.Width / 2 - (int)sizeText.Width / 2) - 15;
        //Rectangle recbutton = new Rectangle(nowX, cellBounds.Location.Y + (cellBounds.Height - (int)sizeText.Height) / 2, (int)sizeText.Width + 5, (int)sizeText.Height + 2);

        //if (IsInBlockRect(e.X, e.Y, recbutton)) // 鼠标移动块区域
        //{
        //    PrivatePaint(graphics, paintCellBounds, newrowIndex, newcolumnindex, 
        //        this.DataGridView.Rows[newrowIndex].DefaultCellStyle, true,
        //        DataGridView[newcolumnindex, newrowIndex].Value.ToString(), BlockClickColor);
        //}
        //else
        //{

        //    BlockCellSingleton.Instance.newRowIndex = -1;
        //    BlockCellSingleton.Instance.newColumnIndex = -1;
        //}
        //for (int i = 0; i < this.DataGridView.Rows.Count; i++)
        //{
        //    for (int j = 0; j < this.DataGridView.Rows[i].Cells.Count; j++)
        //    {
        //        DataGridViewColorBlockCell imageAndDataCell = this.DataGridView.Rows[i].Cells[j] as DataGridViewColorBlockCell;
        //        if (imageAndDataCell == null)
        //        { continue; }
        //        if (i == this.DataGridView.CurrentRow.Index && j == this.DataGridView.CurrentCell.ColumnIndex)
        //        { continue; }
        //        #region 旧单元格
                
        //        Console.WriteLine("去除老的");
        //        Rectangle oldcellBounds = DataGridView[j, i].ContentBounds;
        //        Rectangle oldpaintCellBounds = DataGridView.GetCellDisplayRectangle(j, i, true);
        //        oldpaintCellBounds.Width = DataGridView.Columns[j].Width;
        //        oldpaintCellBounds.Height = DataGridView.Rows[i].Height;
        //        if (DataGridView[j, i].Value == null)
        //        {
        //            continue;
        //        }
        //        SizeF oldsizeText = graphics.MeasureString(DataGridView[j, i].Value.ToString(), _font);   //取字符串按字体时的大小
        //        int oldX = oldcellBounds.Location.X + (oldcellBounds.Width / 2 - (int)oldsizeText.Width / 2) - 15;
        //        Rectangle oldrecbutton = new Rectangle(oldX, oldcellBounds.Location.Y + (oldcellBounds.Height - (int)oldsizeText.Height) / 2, (int)oldsizeText.Width + 5, (int)oldsizeText.Height + 2);
        //        PrivatePaint(graphics, oldpaintCellBounds, i, j, this.DataGridView.Rows[i].DefaultCellStyle, true, DataGridView[j, i].Value.ToString(), BlockBackColor);
        //        #endregion
        //    }
        //}
    }
    /// <summary>
    /// 判断鼠标坐标是否在指定的区域内。
    /// </summary>
    public bool IsInBlockRect(int x, int y, Rectangle area)
    {
        if (x > area.Left && x < area.Right && y > area.Top && y < area.Bottom)
            return true;
        return false;

    }

    protected override void OnMouseMove(DataGridViewCellMouseEventArgs e)
    {
        //Rectangle cellBounds = DataGridView[e.ColumnIndex, e.RowIndex].ContentBounds;
        //int nowX=cellBounds.Location.X + (cellBounds.Width - cellBounds.Width - 16) / 2;
        //Rectangle recbutton = new Rectangle(nowX, cellBounds.Location.Y + 2, cellBounds.Width - 16, cellBounds.Height - 8);

        //if (IsInBlockRect(e.X, e.Y, recbutton)) // 鼠标移动块区域
        //{
        //    this.Cursor = Cursors.Hand;
        //}
        //else
        //{
            
        //}
    }




}

[Serializable]
public class DataGridViewColorBlockCellModel
{

    private Color _headerBackColor = new Color();
    /// <summary>
    /// 表头圆圈背景色    
    /// </summary>
    public Color HeaderBackColor { get { return _headerBackColor; } set { _headerBackColor = value; } }

    private Color _headerFontColor = new Color();
    /// <summary>
    /// 表头字体颜色
    /// </summary>
    public Color HeaderFontColor { get { return _headerFontColor; } set { _headerFontColor = value; } }

    private string _headerFontText = "";
    /// <summary>
    /// 表头圆形内文字
    /// </summary>
    public string HeaderFontText { get { return _headerFontText; } set { _headerFontText = value; } }


    private bool _canShowCircular = true;
    public bool CanSowCircular
    {
        get { return _canShowCircular; }
        set { _canShowCircular = value; }
    }


    private Color _blockBackColor = Color.FromArgb(28, 132, 198);
    /// <summary>
    /// 块 默认背景色
    /// </summary>
    public Color BlockBackColor { get { return _blockBackColor; } set { _blockBackColor = value; } }


    private Color _blockFontColor = Color.White;
    /// <summary>
    /// 块 默认前景色
    /// </summary>
    public Color BlockFontColor { get { return _blockFontColor; } set { _blockFontColor = value; } }

    private Color _blockNoFontColor = Color.Black;
    /// <summary>
    /// 不可用的块 前景色
    /// </summary>
    public Color BlockNoFontColor { get { return _blockNoFontColor; } set { _blockNoFontColor = value; } }

    private Color _blockClickColor = Color.FromArgb(255, 128, 30);
    /// <summary>
    /// 块 点击时颜色
    /// </summary>
    public Color BlockClickColor { get { return _blockClickColor; } set { _blockClickColor = value; } }


    private Color _blockClickFontColor = Color.White;
    /// <summary>
    /// 块点击时前景颜色
    /// </summary>
    public Color BlockClickFontColor { get { return _blockClickFontColor; } set { _blockClickFontColor = value; } }


    private List<Color> _fontColr = new List<Color>();
    [Bindable(false), Browsable(false)]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public List<Color> FontColor { get { return _fontColr; } set { _fontColr = value; } }

    private bool _canChangeColor = true;
    /// <summary>
    /// 是否应用颜色  同时控制默认颜色和点击时颜色
    /// </summary>
    public bool CanChangeColor { get { return _canChangeColor; } set { _canChangeColor = value; } }

    /// <summary>
    /// 字段名称  与 列名称相同
    /// </summary>
    public string ColumnName { get; set; }

    private int _buttonWidth = 0;
    /// <summary>
    /// 列中按钮的宽度
    /// </summary>
    public int ButtonWidth { get { return _buttonWidth; } set { _buttonWidth = value; } }

    private bool _showCircular = true;
    public bool ShowCircular { get { return _showCircular; } set { _showCircular = value; } }


}
已标记关键词 清除标记
相关推荐
开发环境为vs2012 C#源码及完全样例 此版本为2012.2.24日更新版本,更新内容为 * 最后修改时间:2012.2.24 增加了汇总信息的自定义控制属性,参见功能描述12 * 2012.2.23 修改了过多列时父标题不显示的问题,重新写了算法,修改了标题头重叠及不能换行的问题,参见说明11. 总功能列举如下: /**************************************************************************************************** * Copyright (C) 2012 明振居士 版权没有,任意拷贝及使用,但对使用造成的任何后果不负任何责任,互相开源影响,共同进步 * 文 件 名:DataGridViewEx.cs * 创 建 人:明振居士 * Email:nzj.163@163.com qq:342155124 * 创建时间:2010-06-01 * 最后修改时间:2012.2.24 增加了汇总信息的自定义控制属性,参见功能描述12 * 2012.2.23 修改了过多列时父标题不显示的问题,重新写了算法,修改了标题头重叠及不能换行的问题,参见说明11. * 2012-1-19 增加第10条所示的功能;修改了列头超过26列的错误,导出excel为数组方式,速度更快,导出的单元格设置为文本格式。 * 标 题:用户自定义的DataGridView控件 * 功能描述:扩展DataGridView控件功能 * 扩展功能: * 1、搜索Search(); 有两个同明方法,参数不同 F3为快捷键继续向下搜索 * 2、用TreeView HeadSource 来设置复杂的标题样式,如果某个节点对应的显示列隐藏,请将该节点Tag设置为hide,隐藏列的排列位置与绑定数据元列位置对应,树叶节点的顺序需要与结果集的列顺序一致 * 3、通过反射导出Excel,无需引用com组件,方法ExportExcel() ,不受列数的限制,表头同样可以导出,AutoFit属性设置导出excel后是否自动调整单元格宽度 * 导出内容支持自定义的:Title List<string> Header List<string> Footer,支持在设计时值的设定,窗口关闭时Excel资源自动彻底释放 * 4、可以自己任意设定那些列显示及不显示,通过调用方法SetColumnVisible()实现。 * 5、设置列标题SetHeader(),设置列永远可见AlwaysShowCols(),设置列暂时不可见HideCols() * 注意,当使用了TreeView作为复杂Header时,不要使用本方法,Header显示的内容根据treeview内容而显示 * 6、列宽度及顺序的保存SaveGridView(),加载LoadGridView() * 7、支持所见即所得的打印功能,举例如下 * private void button5_Click(object sender, EventArgs e) { DGVPrinter printer = new DGVPrinter(); printer.PrintPreviewDataGridView(DataGridViewEx1); } * 8、自定义合并行与列,行合并用 MergeRowColumn 属性,列合并用MergeColumnNames属性,都可以定义多个列 * 9、行标号的设置 bool ShowRowNumber; * 10、增加最后一行的汇总行,支持列的聚合函数,参见http://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.[removed]v=VS.100).aspx * 假设对id列显示“合计”字符,avgPrice进行平均值,total列显示合计,则对ComputeColumns属性增加三行内容:id,合计:;avgPrice,Avg(avgPrice);total,Sum(total) * 如果需要对值进行格式控制,请实现beforeShow事件 * 增加了导出和打印对应的支持,所见即所得的对齐方应用于式导出及打印。 * 11、列标题头自动换行,默认设置为每个列标题头行高为22,如果设置的字体更大,将自动进行计算。如果需要多行显示,请根据需要调节ColumnHeaderHeight属性值, * 其估算方法为预计的标题头层数乘以22,或层数乘以设置的字体高度。 * 12、针对第10条功能,增加汇总信息的自定义控制属性,汇总字体SummaryFont,汇总字体颜色SummaryFontColor,汇总背景色SummaryBGColor,边框与整个表格边框一致 ****************************************************************************************************/
*调整导出及打印的格式与显示一致;合计行,详细参见第10条修正功能,全部源码及示例。 * 文 件 名:DataGridViewEx.cs * 创 建 人:明振居士 * Email:nzj.163@163.com qq:342155124 * 创建时间:2010-06-01 * 最后修改时间:2012-1-19 增加第10条所示的功能;修改了列头超过26列的错误,导出excel为数组方式,速度更快,导出的单元格设置为文本格式。 * 标 题:用户自定义的DataGridView控件 * 功能描述:扩展DataGridView控件功能 * 扩展功能: * 1、搜索Search(); 有两个同明方法,参数不同 F3为快捷键继续向下搜索 * 2、用TreeView HeadSource 来设置复杂的标题样式,如果某个节点对应的显示列隐藏,请将该节点Tag设置为hide,隐藏列的排列位置与绑定数据元列位置对应,树叶节点的顺序需要与结果集的列顺序一致 * 3、通过反射导出Excel,无需引用com组件,方法ExportExcel() ,不受列数的限制,表头同样可以导出,AutoFit属性设置导出excel后是否自动调整单元格宽度 * 导出内容支持自定义的:Title List<string> Header List<string> Footer,支持在设计时值的设定,窗口关闭时Excel资源自动彻底释放 * 4、可以自己任意设定那些列显示及不显示,通过调用方法SetColumnVisible()实现。 * 5、设置列标题SetHeader(),设置列永远可见AlwaysShowCols(),设置列暂时不可见HideCols() * 注意,当使用了TreeView作为复杂Header时,不要使用本方法,Header显示的内容根据treeview内容而显示 * 6、列宽度及顺序的保存SaveGridView(),加载LoadGridView() * 7、支持所见即所得的打印功能,举例如下 * private void button5_Click(object sender, EventArgs e) { DGVPrinter printer = new DGVPrinter(); printer.PrintPreviewDataGridView(DataGridViewEx1); } * 8、自定义合并行与列,行合并用 MergeRowColumn 属性,列合并用MergeColumnNames属性,都可以定义多个列 * 9、行标号的设置 bool ShowRowNumber; * 10、增加最后一行的汇总行,支持列的聚合函数,参见http://msdn.microsoft.com/zh-cn/library/system.data.datacolumn.[removed]v=VS.100).aspx * 假设对id列显示“合计”字符,avgPrice进行平均值,total列显示合计,则对ComputeColumns增加三行内容:id,合计:;avgPrice,Avg(avgPrice);total,Sum(total) * 如果需要对值进行格式控制,请实现beforeShow事件 * 增加了导出和打印对应的支持,所见即所得的对齐方应用于式导出及打印。
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页