实例介绍
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TestDataGridView
{
public partial class ExDGV : DataGridView
{
public ExDGV()
{
InitializeComponent();
}
#region 表头的属性构造
private string[] _titleHeader=new string [0];
public TopRow topRow;
[Description("添加合并列表头,格式:合并列起始位置从1开始|列名称|占列个数")]
public string[] TitleHeader
{
get { return this._titleHeader; }
set
{
this._titleHeader=value;
this.SetHeader();
}
}
#endregion
#region TCell 单元格属性
public class TCell
{
private int _ColSpan;
private bool _IsColSpan;
private bool _isMiddle;
private int _RelateIndex;
private int _SpanRowWith;
private string _Text;
public TCell()
{ }
public int ColSpan
{//跨度(合并格数)
get { return this._ColSpan; }
set { this._ColSpan = value; }
}
public bool IsColSpan
{//是否要合并
get { return this._IsColSpan; }
set { this._IsColSpan = value; }
}
public int RelateIndex
{
get { return this._RelateIndex; }
set { this._RelateIndex = value; }
}
public bool IsMiddle
{
get { return this._isMiddle; }
set { this._isMiddle = value; }
}
public int SpanRowWith
{
get { return this._SpanRowWith; }
set { this._SpanRowWith = value; }
}
public string Text
{
get { return this._Text; }
set { this._Text = value; }
}
}
#endregion
#region TopRow
public class TopRow
{
public ExDGV.TCell[] Cells;
public TopRow(int colCount)
{
this.Cells = new TCell[colCount];
for (int i = 0; i < colCount; i )
{
this.Cells[i] = new TCell();
}
}
}
#endregion
#region setHeader
/// <summary>
/// 设置表头属性
/// </summary>
private void SetHeader()
{
if (this.TitleHeader != null &&TitleHeader.Length>0)
{
for (int i = 0; i < this.TitleHeader.GetLength(0); i )
{
string[] strArray = this.TitleHeader[i].Split(new char[] { '|' });
int index = Convert.ToInt32(strArray[0]) - 1;
this.topRow.Cells[index].Text = strArray[1];
this.topRow.Cells[index].ColSpan = Convert.ToInt32(strArray[2]);
this.topRow.Cells[index].IsColSpan = true;
for (int j = 1; j < Convert.ToInt32(strArray[2]); j )
{
this.topRow.Cells[index j].Text = strArray[1];
this.topRow.Cells[index j].IsColSpan = true;
if (j != (Convert.ToInt32(strArray[2]) - 1))
{
this.topRow.Cells[index j].IsMiddle = true;
}
}
this.topRow.Cells[(index Convert.ToInt32(strArray[2])) - 1].RelateIndex = index;
}
}
base.Invalidate();
}
#endregion
/// <summary>
/// 得到合并后列表头的宽度
/// </summary>
/// <param name="idx"></param>
/// <param name="self"></param>
/// <returns></returns>
private int GetSpanWidth(int idx,int self)
{
int num = 0;
for (int i = idx self; i < (idx this.topRow.Cells[idx].ColSpan); i )
{
num = base.Columns[i].Width;
}
return num;
}
#region 重载列数增加事件
protected override void OnColumnAdded(DataGridViewColumnEventArgs e)
{
this.topRow = new TopRow(base.Columns.Count);
base.OnColumnAdded(e);
}
#endregion
#region 单元格重绘事件
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
if (((this.TitleHeader!=null) && (e.RowIndex==-1)) && (this.TitleHeader.GetLength(0)>0))
{
new Rectangle(e.CellBounds.X 1, e.CellBounds.Y 1, e.CellBounds.Width - 4, e.CellBounds.Height - 4);
using (Brush brush=new SolidBrush(base.GridColor))
{
using (Brush brush2=new SolidBrush(e.CellStyle.BackColor))
{
using (Pen pen = new Pen(brush))
{
e.Graphics.FillRectangle(brush2, e.CellBounds);
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
if (((e.ColumnIndex > -1) && (this.topRow != null)) && ((this.topRow.Cells[e.ColumnIndex].ColSpan > 1) || this.topRow.Cells[e.ColumnIndex].IsMiddle))
{
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top e.ClipBounds.Height / 2, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
else
{//画整条竖线
e.Graphics.DrawLine(pen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
}
int num1 = e.CellBounds.Height / 3;
if ((e.ColumnIndex > -1) && this.topRow.Cells[e.ColumnIndex].IsColSpan)
{//画中线
int num2 = e.CellBounds.Height / 2;
e.Graphics.DrawLine(pen, e.CellBounds.Left, e.CellBounds.Bottom - (e.CellBounds.Height / 2), e.CellBounds.Right, e.CellBounds.Bottom - (e.CellBounds.Height / 2));
}
if (((e.ColumnIndex > -1) && (this.topRow.Cells[e.ColumnIndex].RelateIndex > 0)) && (this.topRow.Cells[e.ColumnIndex].Text != null))
{//写合并列表头的名称
Rectangle layoutRectangle = new Rectangle((e.CellBounds.X - 1) - this.GetSpanWidth(this.topRow.Cells[e.ColumnIndex].RelateIndex, 1), e.CellBounds.Y 5, this.GetSpanWidth(this.topRow.Cells[e.ColumnIndex].RelateIndex, 0), e.CellBounds.Height / 2);
StringFormat format = new StringFormat();
format.Alignment = StringAlignment.Center;
e.Graphics.DrawString(this.topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Crimson, layoutRectangle, format);
}
float y = 0f;
if (e.ColumnIndex >= 0)
{
if (this.topRow.Cells[e.ColumnIndex].IsColSpan)
{
y = (e.CellBounds.Y (e.CellBounds.Height / 2)) (((e.CellBounds.Height / 2) - e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height) / 2f);
}
else
{
y = e.CellBounds.Y ((e.CellBounds.Height - e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height) / 2f);
}
if (e.Value != null)
{
e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font, Brushes.Crimson, e.CellBounds.X ((e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width) / 2f), y, StringFormat.GenericDefault);
}
}
e.Paint(e.ClipBounds, DataGridViewPaintParts.SelectionBackground | DataGridViewPaintParts.Focus | DataGridViewPaintParts.ErrorIcon | DataGridViewPaintParts.ContentBackground);
e.Handled = true;
}
}
}
}
base.OnCellPainting(e);
}
#endregion
protected override void OnPaint(PaintEventArgs pe)
{
// TODO: 在此处添加自定义绘制代码
//pe.Graphics.TranslateTransform(, this.AutoScrollPosition.Y);//确保滚动时的相对坐标正确
// 调用基类 OnPaint
base.OnPaint(pe);
}
}
}
标签: GridView 实例 C# DataGridView
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论