datagridview 11添加二维表头
2010-04-24 17:59
441 查看
代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class DataGridViewHelper
{
public DataGridViewHelper(DataGridView gridview)
{
gridview.CellPainting += new DataGridViewCellPaintingEventHandler(gridview_CellPainting);
}
int top = 0;
int left = 0;
int height = 0;
int width1 = 0;
void gridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
#region 重绘datagridview表头
DataGridView dgv = (DataGridView)(sender);
if (e.RowIndex != -1) return;
foreach (TopHeader item in Headers)
{
if (e.ColumnIndex >= item.Index && e.ColumnIndex < item.Index + item.Span)
{
if (e.ColumnIndex == item.Index)
{
top = e.CellBounds.Top;
left = e.CellBounds.Left;
height = e.CellBounds.Height;
}
int width = 0;
for (int i = item.Index; i < item.Span +item.Index ; i++)
{
width += dgv.Columns[i].Width;
}
Rectangle rect = new Rectangle(left, top, width, e.CellBounds.Height);
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect);
}
using (Pen gridLinePen = new Pen(dgv.GridColor))
{
e.Graphics.DrawLine(gridLinePen, left, top, left +width , top);
e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left +width , top + height /
2);
width1 = 0;
e.Graphics.DrawLine(gridLinePen, left , top , left , top + height);
for (int i = item.Index; i < item.Span + item.Index; i++)
{
width1 +=dgv.Columns[i].Width;
e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);
}
SizeF sf = e.Graphics.MeasureString(item.Text, e.CellStyle.Font);
float lstr = (width - sf.Width) / 2;
float rstr = (height / 2 - sf.Height) / 2;
//画出文本框
if (item.Text != "")
{
e.Graphics.DrawString(item.Text, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left + lstr,
top + rstr,
StringFormat.GenericDefault);
}
width = 0;
width1 = 0;
for (int i = item.Index; i < item.Span +item.Index ; i++)
{
string columnValue = dgv.Columns[i].HeaderText;
width1 = dgv.Columns[i].Width;
sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
lstr = (width1 - sf.Width) / 2;
rstr = (height / 2 - sf.Height) / 2;
if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left +width + lstr,
top + height / 2 + rstr,
StringFormat.GenericDefault);
}
width += dgv.Columns[i].Width;
}
}
e.Handled = true;
}
}
#endregion
}
private List<TopHeader> _headers = new List<TopHeader>();
public List<TopHeader> Headers
{
get { return _headers; }
}
public struct TopHeader
{
public TopHeader(int index,int span,string text)
{
this.Index = index;
this.Span = span;
this.Text = text;
}
public int Index;
public int Span ;
public string Text;
}
}
}
使用
DataGridViewHelper helper = new DataGridViewHelper(dataGridView1);
helper.Headers.Add(new DataGridViewHelper.TopHeader(0,2,"方案"));
helper.Headers.Add(new DataGridViewHelper.TopHeader(2, 2, "方案3"));
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class DataGridViewHelper
{
public DataGridViewHelper(DataGridView gridview)
{
gridview.CellPainting += new DataGridViewCellPaintingEventHandler(gridview_CellPainting);
}
int top = 0;
int left = 0;
int height = 0;
int width1 = 0;
void gridview_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
{
#region 重绘datagridview表头
DataGridView dgv = (DataGridView)(sender);
if (e.RowIndex != -1) return;
foreach (TopHeader item in Headers)
{
if (e.ColumnIndex >= item.Index && e.ColumnIndex < item.Index + item.Span)
{
if (e.ColumnIndex == item.Index)
{
top = e.CellBounds.Top;
left = e.CellBounds.Left;
height = e.CellBounds.Height;
}
int width = 0;
for (int i = item.Index; i < item.Span +item.Index ; i++)
{
width += dgv.Columns[i].Width;
}
Rectangle rect = new Rectangle(left, top, width, e.CellBounds.Height);
using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
{
//抹去原来的cell背景
e.Graphics.FillRectangle(backColorBrush, rect);
}
using (Pen gridLinePen = new Pen(dgv.GridColor))
{
e.Graphics.DrawLine(gridLinePen, left, top, left +width , top);
e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left +width , top + height /
2);
width1 = 0;
e.Graphics.DrawLine(gridLinePen, left , top , left , top + height);
for (int i = item.Index; i < item.Span + item.Index; i++)
{
width1 +=dgv.Columns[i].Width;
e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);
}
SizeF sf = e.Graphics.MeasureString(item.Text, e.CellStyle.Font);
float lstr = (width - sf.Width) / 2;
float rstr = (height / 2 - sf.Height) / 2;
//画出文本框
if (item.Text != "")
{
e.Graphics.DrawString(item.Text, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left + lstr,
top + rstr,
StringFormat.GenericDefault);
}
width = 0;
width1 = 0;
for (int i = item.Index; i < item.Span +item.Index ; i++)
{
string columnValue = dgv.Columns[i].HeaderText;
width1 = dgv.Columns[i].Width;
sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
lstr = (width1 - sf.Width) / 2;
rstr = (height / 2 - sf.Height) / 2;
if (columnValue != "")
{
e.Graphics.DrawString(columnValue, e.CellStyle.Font,
new SolidBrush(e.CellStyle.ForeColor),
left +width + lstr,
top + height / 2 + rstr,
StringFormat.GenericDefault);
}
width += dgv.Columns[i].Width;
}
}
e.Handled = true;
}
}
#endregion
}
private List<TopHeader> _headers = new List<TopHeader>();
public List<TopHeader> Headers
{
get { return _headers; }
}
public struct TopHeader
{
public TopHeader(int index,int span,string text)
{
this.Index = index;
this.Span = span;
this.Text = text;
}
public int Index;
public int Span ;
public string Text;
}
}
}
使用
DataGridViewHelper helper = new DataGridViewHelper(dataGridView1);
helper.Headers.Add(new DataGridViewHelper.TopHeader(0,2,"方案"));
helper.Headers.Add(new DataGridViewHelper.TopHeader(2, 2, "方案3"));
相关文章推荐
- DataGridView 表头中添加过滤列表(类似Excel表头过滤)
- DataGridView单元格合并和二维表头
- 在C/S架构中,给DataGridView的表头添加CheckBox控件
- [转]Datagridview 实现二维表头
- .NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤
- Datagridview 实现二维表头和行合并【转载】
- Datagridview 实现二维表头
- winform 中 给DataGridView的表头添加CheckBox
- 在Winform怎么给DataGridView的表头添加CheckBox?好逗
- NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤【转载】
- c#datagridview实现二维表头
- Datagridview 实现二维表头
- DataGridView扩展的一些想法(二维表头、合并单元格、合计行)
- winform 中 给DataGridView的表头添加CheckBox
- DataGridView合并表头实现 、二维表头的实现
- winform 中 给DataGridView的表头添加CheckBox
- Datagridview 实现二维表头
- .NET WinForm程序中给DataGridView表头添加下拉列表实现数据过滤
- DataGridView合并表头实现 、二维表头的实现、单元格合并
- [转]Datagridview 实现二维表头