重写DataGridView

本文介绍了一种自定义DataGridView控件的方法,通过重写DataGridView类实现。该控件改进了行头显示方式,并增强了绘制效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//#===========================================================================//
//# 
//#  类名称            : MyDataGridView
//#  机能概要          : 重写DataGridView
//#  作成者            :
//#  做成日            : 2009/07/01
//#  版本              : 1.0.0.0
//# <变更履历>
//#  版本   日期            变更者          变更内容
//#  -------------------------------------------------------------------------
//# 1.0.0.0 2009/07/01               做成
//#
//#
//#===========================================================================// 
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;

namespace control
{
    class MyDataGridView:System.Windows.Forms.DataGridView
    {
        private System.Windows.Forms.Control controls = null;       

        public MyDataGridView()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            this.controls = new System.Windows.Forms.Control();
            ((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
            this.SuspendLayout();
            //
            // controls
            //
            this.controls.Location = new System.Drawing.Point(0, 0);
            this.controls.Name = "controls";
            this.controls.Size = new System.Drawing.Size(0, 0);
            this.controls.TabIndex = 0;
            //
            // MyDataGridView
            //
            this.ColumnHeadersHeight = 25;
            this.RowTemplate.Height = 23;
            this.RowPostPaint += new System.Windows.Forms.DataGridViewRowPostPaintEventHandler(this.MyDataGridView_RowPostPaint);
            this.Paint += new System.Windows.Forms.PaintEventHandler(this.MyDataGridView_Paint);
            ((System.ComponentModel.ISupportInitialize)(this)).EndInit();
            this.ResumeLayout(false);

        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (controls != null)
                {
                    controls.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        private void MyDataGridView_RowPostPaint(object sender, System.Windows.Forms.DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture)
                , this.Font, b, e.RowBounds.Location.X + this.RowHeadersWidth / 2
                , e.RowBounds.Location.Y + (e.RowBounds.Height-this.Font.Height) / 2);        
       
        }

        private void MyDataGridView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (this.Columns.Count>0 && this.ColumnHeadersVisible == true && this.RowHeadersVisible==true)
            {
                SolidBrush b = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
                e.Graphics.DrawString("序号", this.Font, b, this.RowHeadersWidth / 2 - this.Font.SizeInPoints
                        , (this.ColumnHeadersHeight - this.Font.Height) / 2);              
            }
       
        }
      
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值