自定义DataGridView控件

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Windows.Forms;

namespace CustomControls
{
    /// <summary>
    /// 自定义DataGridView控件
    /// </summary>
    public class CustomDataGirdView : System.Windows.Forms.DataGridView
    {

        #region 重绘Column、Row

        int _RowHeadWidth = 41;
        /// <summary>
        /// 重绘Column、Row
        /// </summary>
        /// <param name="e"></param>
        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
                //如果是Column
                if(e.RowIndex == -1) {
                    drawColumnAndRow(e);
                    e.Handled = true;
                    //如果是Rowheader
                } else if(e.ColumnIndex < 0 && e.RowIndex>=0) {
                    drawColumnAndRow(e);
                    _RowHeadWidth = e.CellBounds.Width;
                    e.Handled = true;
               }
        }

        /// <summary>
        /// Column和RowHeader绘制
        /// </summary>
        /// <param name="e"></param>
        void drawColumnAndRow(DataGridViewCellPaintingEventArgs e)
        {
            // 绘制背景色
            using (LinearGradientBrush backbrush =
                new LinearGradientBrush(e.CellBounds,
                    ProfessionalColors.MenuItemPressedGradientBegin,
                    ProfessionalColors.MenuItemPressedGradientMiddle 
                    , LinearGradientMode.Vertical)){
           
                Rectangle border = e.CellBounds;
                border.Width -= 1;
                //填充绘制效果
                e.Graphics.FillRectangle(backbrush, border);
                //绘制Column、Row的Text信息
                e.PaintContent(e.CellBounds);  // 参数的意义?
                //绘制边框
                ControlPaint.DrawBorder3D(e.Graphics, e.CellBounds, Border3DStyle.Etched);

            }
        }

        #endregion


        #region 重绘选中状态

        #region Row重绘前处理

        /// <summary>
        /// Row重绘前处理,绘制行样式
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
        {
            // 请务必调用基类的 OnRowPrePaint 方法,以便已注册的委托能够接收到该事件
            base.OnRowPrePaint(e);

            //是否是选中状态
            if ((e.State & DataGridViewElementStates.Selected) ==
                        DataGridViewElementStates.Selected)
            {
                // 计算选中区域Size
                int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth;

                Rectangle rowBounds = new Rectangle(
                  0, e.RowBounds.Top, width,
                    e.RowBounds.Height);

                // 绘制选中背景色
                using (LinearGradientBrush backbrush =
                    new LinearGradientBrush(rowBounds,
                        Color.SteelBlue,
                        e.InheritedRowStyle.ForeColor, 90.0f))
                {
                    e.Graphics.FillRectangle(backbrush, rowBounds);
                    e.PaintCellsContent(rowBounds);
                    e.Handled = true;       //告诉系统,已经自己重绘过了,该次绘制任务到此结束
                }

            }
        }

        #endregion

        #region Row重绘后处理

        /// <summary>
        /// Row重绘后处理,目前显示效果不大
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRowPostPaint(DataGridViewRowPostPaintEventArgs e)
        {
            // 请务必调用基类的 OnRowPostPaint 方法,以便已注册的委托能够接收到该事件
            base.OnRowPostPaint(e);
            int width = this.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) + _RowHeadWidth;
            Rectangle rowBounds = new Rectangle(
                   0, e.RowBounds.Top, width, e.RowBounds.Height);

            if (this.CurrentCellAddress.Y == e.RowIndex)
            {
                //设置选中边框,显示为虚线的聚焦框
                e.DrawFocus(rowBounds, true);   
            }
        }

        #endregion

        #region Row刷新
        /// <summary>
        /// 宽度改变后处理,暂时没出现效果
        /// </summary>
        protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
        {
            base.OnColumnWidthChanged(e);
            if (this.CurrentRow != null)
                this.InvalidateRow(this.CurrentRow.Index);
        }

        /// <summary>
        /// 用户或代码滚动工作区时发生,暂没看见效果
        /// </summary>
        protected override void OnScroll(ScrollEventArgs e)
        {
            base.OnScroll(e);
            if (this.CurrentRow != null)
                this.InvalidateRow(this.CurrentRow.Index);
        }

        #endregion

        #endregion


    }
}
注意引用命名空间

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值