Datagridview 实现二维表头

原文地址为:Datagridview 实现二维表头

      最近把我们的b/s系统,增加智能客户端的功能。确实智能客户端是非常好用的东西。可惜winform的控件功能不怎么强大,相比vb差很多啊。比如DataGridView不支持二维表头,不支持表尾合计,相比之下 web的好办多了(还是喜欢Web的排版、导航,但喜欢Win的操作性,希望WPF早日流行)。

       但是 MIS系统没有二维表头确实是客户不能接受的,尝试了com控件flexgrid或者开源的SourceGrid3,但都不怎么好用,于是想改造一下DataGridView。我的做法是在CellPainting做手脚。花了一天时间尝试,只是做出原型,还没有完善,希望有需要的朋友少走弯路。

  1,继承DataGridView,添加表头信息类。
  2,添加CellPainting,代码如下:
Datagridview 实现二维表头   private   void  DataGridViewEx_CellPainting( object  sender, DataGridViewCellPaintingEventArgs e)
Datagridview 实现二维表头Datagridview 实现二维表头        
Datagridview 实现二维表头 {
Datagridview 实现二维表头            
if (e.RowIndex == -1)
Datagridview 实现二维表头Datagridview 实现二维表头            
Datagridview 实现二维表头{
Datagridview 实现二维表头             
//   int w = dataGridView1.HorizontalScrollingOffset + dataGridView1.TopLeftHeaderCell.Size.Width + dataGridView1.Columns[0].Width + 10;
Datagridview 实现二维表头

Datagridview 实现二维表头
Datagridview 实现二维表头                Rectangle newRect 
= new Rectangle(e.CellBounds.X + 1,
Datagridview 实现二维表头               e.CellBounds.Y 
+ 1, e.CellBounds.Width - 4,
Datagridview 实现二维表头               e.CellBounds.Height 
- 4);
Datagridview 实现二维表头
Datagridview 实现二维表头                
using (
Datagridview 实现二维表头                    Brush gridBrush 
= new SolidBrush(this.GridColor),
Datagridview 实现二维表头                    backColorBrush 
= new SolidBrush(e.CellStyle.BackColor))
Datagridview 实现二维表头Datagridview 实现二维表头                
Datagridview 实现二维表头{
Datagridview 实现二维表头                    
using (Pen gridLinePen = new Pen(gridBrush))
Datagridview 实现二维表头Datagridview 实现二维表头                    
Datagridview 实现二维表头{
Datagridview 实现二维表头                        
// Erase the cell.
Datagridview 实现二维表头
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
Datagridview 实现二维表头
Datagridview 实现二维表头                        
// Draw the grid lines (only the right and bottom lines;
Datagridview 实现二维表头                        
// DataGridView takes care of the others).
Datagridview 实现二维表头
                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left,
Datagridview 实现二维表头                            e.CellBounds.Bottom 
- 1, e.CellBounds.Right - 1,
Datagridview 实现二维表头                            e.CellBounds.Bottom 
- 1);
Datagridview 实现二维表头                        
if (e.ColumnIndex > -1 && topRow!=null&&topRow.Cells[e.ColumnIndex].ColSpan>1)
Datagridview 实现二维表头Datagridview 实现二维表头                        
Datagridview 实现二维表头{
Datagridview 实现二维表头                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right 
- 1,
Datagridview 实现二维表头                                e.CellBounds.Top 
+ e.ClipBounds.Height / 2, e.CellBounds.Right - 1,
Datagridview 实现二维表头                                e.CellBounds.Bottom);
Datagridview 实现二维表头                        }

Datagridview 实现二维表头                        
else
Datagridview 实现二维表头Datagridview 实现二维表头                        
Datagridview 实现二维表头{
Datagridview 实现二维表头                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right 
- 1,
Datagridview 实现二维表头                                                          e.CellBounds.Top, e.CellBounds.Right 
- 1,
Datagridview 实现二维表头                                                          e.CellBounds.Bottom);
Datagridview 实现二维表头                        }

Datagridview 实现二维表头
Datagridview 实现二维表头                        
// Draw the inset highlight box.
Datagridview 实现二维表头                        
//   e.Graphics.DrawRectangle(Pens.Blue, newRect);
Datagridview 实现二维表头

Datagridview 实现二维表头                        
int scale = e.CellBounds.Height/3;
Datagridview 实现二维表头                        
if (e.ColumnIndex > -1 && topRow.Cells[e.ColumnIndex].Text != null)
Datagridview 实现二维表头Datagridview 实现二维表头                        
Datagridview 实现二维表头{
Datagridview 实现二维表头                            scale
= e.CellBounds.Height / 2;
Datagridview 实现二维表头                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom 
- e.CellBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom - e.CellBounds.Height / 2);
Datagridview 实现二维表头                        }

Datagridview 实现二维表头                        
// Draw the text content of the cell, ignoring alignment.
Datagridview 实现二维表头

Datagridview 实现二维表头                      
Datagridview 实现二维表头
Datagridview 实现二维表头                        
if (e.Value != null)
Datagridview 实现二维表头Datagridview 实现二维表头                        
Datagridview 实现二维表头{
Datagridview 实现二维表头                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
Datagridview 实现二维表头                                Brushes.Crimson, e.CellBounds.X 
+ 2,
Datagridview 实现二维表头                                e.CellBounds.Y 
+ scale+ 2, StringFormat.GenericDefault);
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头                        }

Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头                        
if (e.ColumnIndex > -1 &&  topRow.Cells[e.ColumnIndex].RelateIndex > -1 && topRow.Cells[e.ColumnIndex].Text!=null)
Datagridview 实现二维表头                    
Datagridview 实现二维表头Datagridview 实现二维表头                        
Datagridview 实现二维表头{
Datagridview 实现二维表头                            Rectangle recCell 
= new Rectangle(e.CellBounds.X - 1 - topRow.Cells[e.ColumnIndex].SpanRowWith,
Datagridview 实现二维表头           e.CellBounds.Y 
+ 1, topRow.Cells[e.ColumnIndex].SpanRowWith,
Datagridview 实现二维表头           e.CellBounds.Height 
/ 2);
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头                            StringFormat sf 
= new StringFormat();
Datagridview 实现二维表头
Datagridview 实现二维表头                            sf.Alignment 
= StringAlignment.Center;
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头                            e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Crimson, recCell, sf);
Datagridview 实现二维表头
Datagridview 实现二维表头                        }

Datagridview 实现二维表头               
Datagridview 实现二维表头                        e.Handled 
= true;
Datagridview 实现二维表头                    }

Datagridview 实现二维表头                }

Datagridview 实现二维表头            }

Datagridview 实现二维表头
Datagridview 实现二维表头        }

3,调用方法
Datagridview 实现二维表头  dataGridViewEx1.TopRow.Cells[ 2 ].Text  =   " 入库 " ;
Datagridview 实现二维表头            dataGridViewEx1.TopRow.Cells[
2 ].ColSpan  =   2 ;
Datagridview 实现二维表头
Datagridview 实现二维表头
Datagridview 实现二维表头            dataGridViewEx1.TopRow.Cells[
4 ].Text  =   " 出库 " ;
Datagridview 实现二维表头            dataGridViewEx1.TopRow.Cells[
4 ].ColSpan  =   2 ;
4,效果图
Datagridview 实现二维表头

至于表尾合计,也做出了原型。二维表头+表尾合计,基本上满足需求了。

转载请注明本文地址:Datagridview 实现二维表头