Datagridview 实现二维表头
发布时间:2020-12-16 23:10:46 所属栏目:大数据 来源:网络整理
导读:http://www.cnblogs.com/neuhawk/archive/2006/09/28/517507.html Datagridview 实现二维表头 最近把我们的b/s系统,增加智能客户端的功能。确实智能客户端是非常好用的东西。可惜winform的控件功能不怎么强大,相比vb差很多
http://www.cnblogs.com/neuhawk/archive/2006/09/28/517507.html
Datagridview 实现二维表头
最近把我们的b/s系统,增加智能客户端的功能。确实智能客户端是非常好用的东西。可惜winform的控件功能不怎么强大,相比vb差很多啊。比如 DataGridView不支持二维表头,不支持表尾合计,相比之下 web的好办多了(还是喜欢Web的排版、导航,但喜欢Win的操作性,希望WPF早日流行)。
private
void
DataGridViewEx_CellPainting(
object
sender,DataGridViewCellPaintingEventArgse)
{ if (e.RowIndex == - 1 ) { // intw=dataGridView1.HorizontalScrollingOffset+dataGridView1.TopLeftHeaderCell.Size.Width+dataGridView1.Columns[0].Width+10; RectanglenewRect = new Rectangle(e.CellBounds.X + 1 , e.CellBounds.Y + 1 ,e.CellBounds.Width - 4 , e.CellBounds.Height - 4 ); using ( BrushgridBrush = new SolidBrush( this .GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor)) { using (PengridLinePen = new Pen(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); int scale = 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 = new Rectangle(e.CellBounds.X - 1 - topRow.Cells[e.ColumnIndex].SpanRowWith,topRow.Cells[e.ColumnIndex].SpanRowWith, e.CellBounds.Height / 2 ); StringFormatsf = new StringFormat(); sf.Alignment = StringAlignment.Center; e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text,Brushes.Crimson,recCell,sf); } e.Handled = true ; } } } }
dataGridViewEx1.TopRow.Cells[
2
].Text
=
"
入库
"
;
dataGridViewEx1.TopRow.Cells[ 2 ].ColSpan = 2 ; dataGridViewEx1.TopRow.Cells[ 4 ].Text = " 出库 " ; dataGridViewEx1.TopRow.Cells[ 4 ].ColSpan = 2 ; 4,效果图 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |