//#===========================================================================//
//#
//# 类名称 : MyDataGridView
//# 机能概要 : 重写DataGridView
//# 作成者 :
//# 做成日 : 2009/07/01
//# 版本 : 1.0.0.0
//# <变更履历>
//# 版本 日期 变更者 变更内容
//# -------------------------------------------------------------------------
//# 1.0.0.0 2009/07/01 王晓光 做成
//# 1.0.0.1 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
{
#region 定义
//控件
private System.Windows.Forms.Control controls = null;
//表头
private string _TableHeaderText = String.Empty;
#endregion
/// <summary>
/// 构建器
/// </summary>
public MyDataGridView()
{
InitializeComponent();
}
#region 控件属性
/// <summary>
/// 表头
/// </summary>
public string TableHeaderText
{
set
{
this._TableHeaderText = value;
}
get
{
return this._TableHeaderText;
}
}
#endregion
#region 方法
/// <summary>
/// 初始化
/// </summary>
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);
}
/// <summary>
/// 销毁
/// </summary>
/// <param name="disposing">bool</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (controls != null)
{
controls.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// 重写RowPostPaint
/// </summary>
/// <param name="sender">object</param>
/// <param name="e">DataGridViewRowPostPaintEventArgs</param>
private void MyDataGridView_RowPostPaint(object sender, System.Windows.Forms.DataGridViewRowPostPaintEventArgs e)
{ if (this.RowHeadersVisible == true)
{
SolidBrush b = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
//列头文字格式
Font f = this.ColumnHeadersDefaultCellStyle.Font;
//行号
string Content = (e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture);
SizeF ContentSizeF = e.Graphics.MeasureString(Content, f);
//显示行号
e.Graphics.DrawString(Content,this.Font, b
, e.RowBounds.Location.X + (this.RowHeadersWidth - ContentSizeF.Width) / 2
, e.RowBounds.Location.Y + (e.RowBounds.Height - ContentSizeF.Height) / 2);
}
}
/// <summary>
/// 重写Paint
/// </summary>
/// <param name="sender">object</param>
/// <param name="e">PaintEventArgs</param>
private void MyDataGridView_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
if (this.Columns.Count>0 && this.ColumnHeadersVisible == true && this.RowHeadersVisible==true)
{
//列头文字格式
Font f = this.ColumnHeadersDefaultCellStyle.Font;
SizeF TableHeaderTextSizeF = e.Graphics.MeasureString(this.TableHeaderText, f);
if (TableHeaderTextSizeF.Width > this.ColumnHeadersHeight)
{
//处理显示内容
}
SolidBrush b = new SolidBrush(this.RowHeadersDefaultCellStyle.ForeColor);
//显示表头
e.Graphics.DrawString(this.TableHeaderText, f, b
, (this.RowHeadersWidth - TableHeaderTextSizeF.Width) / 2
, (this.ColumnHeadersHeight - TableHeaderTextSizeF.Height) / 2);
}
}
#endregion
}
}