关闭

C#重写DataGirdView

标签: c#源码datagridview重写自定义
1038人阅读 评论(0) 收藏 举报
分类:

首先我们先来看下效果




页面随窗体大小 自动调整大小




重写的DataGirdView 源码

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

namespace 流水
{
    /// <summary>
    /// 自定义DataGridView控件
    /// </summary>
    public class testDataGirdView: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.OverflowButtonGradientBegin,
                    ProfessionalColors.OverflowButtonGradientMiddle 
                    , 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.Flat);

            }
        }

        #endregion


        #region 重绘选中状态

        #region Row重绘前处理

        /// <summary>
        /// Row重绘前处理
        /// </summary>
        /// <param name="e"></param>
        protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
        {
            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.Salmon,
                        Color.Green, 10.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)
        {
            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刷新

        protected override void OnColumnWidthChanged(DataGridViewColumnEventArgs e)
        {
            base.OnColumnWidthChanged(e);
            if (this.CurrentRow != null)
                this.InvalidateRow(this.CurrentRow.Index);
        }

        protected override void OnScroll(ScrollEventArgs e)
        {
            base.OnScroll(e);
            if (this.CurrentRow != null)
                this.InvalidateRow(this.CurrentRow.Index);
        }

        #endregion

        #endregion


    }
}

页面随窗体大小 自动调整大小源码



首先把DataGirdView 控件 Anchor属性 设置为Top, Bottom, Left, Right


在启动事件写如下代码


        public void DataSize()
        {
            //获取dataGridView1的宽度
            int DataWidh = this.dataGridView1.Width;
            //获取dataGridView1的所以列
            int DataLengt = this.dataGridView1.Columns.Count;
            //45为第一个空格
            DataWidh = (DataWidh - 45) / DataLengt;
            dataGridView1.Columns[0].Width = DataWidh;
            dataGridView1.Columns[1].Width = DataWidh;
            dataGridView1.Columns[3].Width = DataWidh;
            dataGridView1.Columns[2].Width = DataWidh;

        }

这样基本上就实现了以上基本效果


希望大家多多支持
本  人 菜 鸟  级
刚   刚  毕   业
希望 通 过 博 客
提升下自己的能力

希望大家给点建议

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10506次
    • 积分:234
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条