但是 MIS系统没有二维表头确实是客户不能接受的,尝试了com控件flexgrid或者开源的SourceGrid3,但都不怎么好用,于是想改造一下DataGridView。我的做法是在CellPainting做手脚。花了一天时间尝试,只是做出原型,还没有完善,希望有需要的朋友少走弯路。
1,继承DataGridView,添加表头信息类。
2,添加CellPainting,代码如下:
private
void
DataGridViewEx_CellPainting(
object
sender,DataGridViewCellPaintingEventArgse)
{
if(e.RowIndex==-1)
{
//intw=dataGridView1.HorizontalScrollingOffset+dataGridView1.TopLeftHeaderCell.Size.Width+dataGridView1.Columns[0].Width+10;
RectanglenewRect=newRectangle(e.CellBounds.X+1,
e.CellBounds.Y+1,e.CellBounds.Width-4,
e.CellBounds.Height-4);
using(
BrushgridBrush=newSolidBrush(this.GridColor),
backColorBrush=newSolidBrush(e.CellStyle.BackColor))
{
using(PengridLinePen=newPen(gridBrush))
{
//Erasethecell.
e.Graphics.FillRectangle(backColorBrush,e.CellBounds);
//Drawthegridlines(onlytherightandbottomlines;
//DataGridViewtakescareoftheothers).
e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,
e.CellBounds.Bottom-1,e.CellBounds.Right-1,
e.CellBounds.Bottom-1);
if(e.ColumnIndex>-1&&topRow!=null&&topRow.Cells[e.ColumnIndex].ColSpan>1)
{
e.Graphics.DrawLine(gridLinePen,
e.CellBounds.Top+e.ClipBounds.Height/2,
e.CellBounds.Bottom);
}
else
{
e.Graphics.DrawLine(gridLinePen,
e.CellBounds.Top,
e.CellBounds.Bottom);
}
//DrawtheinsethighlightBox.
//e.Graphics.DrawRectangle(Pens.Blue,newRect);
intscale=e.CellBounds.Height/3;
if(e.ColumnIndex>-1&&topRow.Cells[e.ColumnIndex].Text!=null)
{
scale=e.CellBounds.Height/2;
e.Graphics.DrawLine(gridLinePen,e.CellBounds.Bottom-e.CellBounds.Height/2,e.CellBounds.Right,e.CellBounds.Bottom-e.CellBounds.Height/2);
}
//Drawthetextcontentofthecell,ignoringalignment.
if(e.Value!=null)
{
e.Graphics.DrawString(e.Value.ToString(),e.CellStyle.Font,
Brushes.Crimson,e.CellBounds.X+2,
e.CellBounds.Y+scale+2,StringFormat.GenericDefault);
}
if(e.ColumnIndex>-1&&topRow.Cells[e.ColumnIndex].RelateIndex>-1&&topRow.Cells[e.ColumnIndex].Text!=null)
{
RectanglerecCell=newRectangle(e.CellBounds.X-1-topRow.Cells[e.ColumnIndex].SpanRowWith,topRow.Cells[e.ColumnIndex].SpanRowWith,
e.CellBounds.Height/2);
StringFormatsf=newStringFormat();
sf.Alignment=StringAlignment.Center;
e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text,Brushes.Crimson,recCell,sf);
}
e.Handled=true;
}
}
}
}
{
if(e.RowIndex==-1)
{
//intw=dataGridView1.HorizontalScrollingOffset+dataGridView1.TopLeftHeaderCell.Size.Width+dataGridView1.Columns[0].Width+10;
RectanglenewRect=newRectangle(e.CellBounds.X+1,
e.CellBounds.Y+1,e.CellBounds.Width-4,
e.CellBounds.Height-4);
using(
BrushgridBrush=newSolidBrush(this.GridColor),
backColorBrush=newSolidBrush(e.CellStyle.BackColor))
{
using(PengridLinePen=newPen(gridBrush))
{
//Erasethecell.
e.Graphics.FillRectangle(backColorBrush,e.CellBounds);
//Drawthegridlines(onlytherightandbottomlines;
//DataGridViewtakescareoftheothers).
e.Graphics.DrawLine(gridLinePen,e.CellBounds.Left,
e.CellBounds.Bottom-1,e.CellBounds.Right-1,
e.CellBounds.Bottom-1);
if(e.ColumnIndex>-1&&topRow!=null&&topRow.Cells[e.ColumnIndex].ColSpan>1)
{
e.Graphics.DrawLine(gridLinePen,
e.CellBounds.Top+e.ClipBounds.Height/2,
e.CellBounds.Bottom);
}
else
{
e.Graphics.DrawLine(gridLinePen,
e.CellBounds.Top,
e.CellBounds.Bottom);
}
//DrawtheinsethighlightBox.
//e.Graphics.DrawRectangle(Pens.Blue,newRect);
intscale=e.CellBounds.Height/3;
if(e.ColumnIndex>-1&&topRow.Cells[e.ColumnIndex].Text!=null)
{
scale=e.CellBounds.Height/2;
e.Graphics.DrawLine(gridLinePen,e.CellBounds.Bottom-e.CellBounds.Height/2,e.CellBounds.Right,e.CellBounds.Bottom-e.CellBounds.Height/2);
}
//Drawthetextcontentofthecell,ignoringalignment.
if(e.Value!=null)
{
e.Graphics.DrawString(e.Value.ToString(),e.CellStyle.Font,
Brushes.Crimson,e.CellBounds.X+2,
e.CellBounds.Y+scale+2,StringFormat.GenericDefault);
}
if(e.ColumnIndex>-1&&topRow.Cells[e.ColumnIndex].RelateIndex>-1&&topRow.Cells[e.ColumnIndex].Text!=null)
{
RectanglerecCell=newRectangle(e.CellBounds.X-1-topRow.Cells[e.ColumnIndex].SpanRowWith,topRow.Cells[e.ColumnIndex].SpanRowWith,
e.CellBounds.Height/2);
StringFormatsf=newStringFormat();
sf.Alignment=StringAlignment.Center;
e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text,Brushes.Crimson,recCell,sf);
}
e.Handled=true;
}
}
}
}
3,调用方法
dataGridViewEx1.TopRow.Cells[
2
].Text
=
"
入库
"
;
dataGridViewEx1.TopRow.Cells[ 2 ].ColSpan = 2 ;
dataGridViewEx1.TopRow.Cells[ 4 ].Text = " 出库 " ;
dataGridViewEx1.TopRow.Cells[ 4 ].ColSpan = 2 ;
4,效果图
dataGridViewEx1.TopRow.Cells[ 2 ].ColSpan = 2 ;
dataGridViewEx1.TopRow.Cells[ 4 ].Text = " 出库 " ;
dataGridViewEx1.TopRow.Cells[ 4 ].ColSpan = 2 ;
至于表尾合计,也做出了原型。二维表头+表尾合计,基本上满足需求了。 原文链接:https://www.f2er.com/vb/260678.html