Datagridview 实现二维表头

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

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

  1,继承DataGridView,添加表头信息类。
  2,添加CellPainting,代码如下:
None.gif    private   void  DataGridViewEx_CellPainting( object  sender, DataGridViewCellPaintingEventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
if (e.RowIndex == -1)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif             
//   int w = dataGridView1.HorizontalScrollingOffset + dataGridView1.TopLeftHeaderCell.Size.Width + dataGridView1.Columns[0].Width + 10;
InBlock.gif

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

InBlock.gif                        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right 
- 1,
InBlock.gif                                                          e.CellBounds.Top, e.CellBounds.Right 
- 1,
InBlock.gif                                                          e.CellBounds.Bottom);
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif                        
// Draw the inset highlight box.
InBlock.gif                        
//   e.Graphics.DrawRectangle(Pens.Blue, newRect);
InBlock.gif

InBlock.gif                        
int scale = e.CellBounds.Height/3;
InBlock.gif                        
if (e.ColumnIndex > -1 && topRow.Cells[e.ColumnIndex].Text != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            scale
= e.CellBounds.Height / 2;
InBlock.gif                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom 
- e.CellBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom - e.CellBounds.Height / 2);
ExpandedSubBlockEnd.gif                        }

InBlock.gif                        
// Draw the text content of the cell, ignoring alignment.
InBlock.gif

InBlock.gif                      
InBlock.gif
InBlock.gif                        
if (e.Value != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            e.Graphics.DrawString(e.Value.ToString(), e.CellStyle.Font,
InBlock.gif                                Brushes.Crimson, e.CellBounds.X 
+ 2,
InBlock.gif                                e.CellBounds.Y 
+ scale+ 2, StringFormat.GenericDefault);
InBlock.gif
InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif
InBlock.gif                        
if (e.ColumnIndex > -1 &&  topRow.Cells[e.ColumnIndex].RelateIndex > -1 && topRow.Cells[e.ColumnIndex].Text!=null)
InBlock.gif                    
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
dot.gif{
InBlock.gif                            Rectangle recCell 
= new Rectangle(e.CellBounds.X - 1 - topRow.Cells[e.ColumnIndex].SpanRowWith,
InBlock.gif           e.CellBounds.Y 
+ 1, topRow.Cells[e.ColumnIndex].SpanRowWith,
InBlock.gif           e.CellBounds.Height 
/ 2);
InBlock.gif
InBlock.gif
InBlock.gif                            StringFormat sf 
= new StringFormat();
InBlock.gif
InBlock.gif                            sf.Alignment 
= StringAlignment.Center;
InBlock.gif
InBlock.gif
InBlock.gif                            e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, Brushes.Crimson, recCell, sf);
InBlock.gif
ExpandedSubBlockEnd.gif                        }

InBlock.gif               
InBlock.gif                        e.Handled 
= true;
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedBlockEnd.gif        }

3,调用方法
None.gif   dataGridViewEx1.TopRow.Cells[ 2 ].Text  =   " 入库 " ;
None.gif            dataGridViewEx1.TopRow.Cells[
2 ].ColSpan  =   2 ;
None.gif
None.gif
None.gif            dataGridViewEx1.TopRow.Cells[
4 ].Text  =   " 出库 " ;
None.gif            dataGridViewEx1.TopRow.Cells[
4 ].ColSpan  =   2 ;
4,效果图
sample.GIF

至于表尾合计,也做出了原型。二维表头+表尾合计,基本上满足需求了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在VB的DataGridView控件中,我们可以通过某些方法来实现二维表头。一种常用的实现方式是使用合并单元格来模拟二维表头。 首先,我们需要创建一个DataGridView控件,并将其属性AllowUserToAddRows设置为False,以防止用户添加新行。然后,我们可以使用以下代码来实现二维表头的合并: ``` ' 合并第一行的两个单元格 DataGridView1.Rows.Add() DataGridView1.Rows(0).Cells(0).Value = "一维表头" DataGridView1.Rows(0).Cells(0).RowSpan = 2 DataGridView1.Rows(0).Cells(1).Value = "二维表头" DataGridView1.Rows(0).Cells(1).ColumnSpan = 3 ' 合并第二行的三个单元格 DataGridView1.Rows.Add() DataGridView1.Rows(1).Cells(1).Value = "列1" DataGridView1.Rows(1).Cells(2).Value = "列2" DataGridView1.Rows(1).Cells(3).Value = "列3" DataGridView1.Rows(1).Cells(1).ColumnSpan = 1 DataGridView1.Rows(1).Cells(2).ColumnSpan = 1 DataGridView1.Rows(1).Cells(3).ColumnSpan = 1 ' 添加数据 DataGridView1.Rows.Add("数据1", "数据2", "数据3") DataGridView1.Rows.Add("数据4", "数据5", "数据6") ``` 以上代码中,我们首先添加了两行数据,第一行为一维表头的内容,第二行为二维表头的内容。然后,使用RowSpan和ColumnSpan属性来合并对应的单元格。 最后,我们可以通过添加数据的方式来展示具体的二维表数据。 通过以上的代码,我们便可以在VB的DataGridView实现一个带有二维表头的表格。需要注意的是,在使用合并单元格时,需要确保合并的单元格数量和位置正确,以免出现布局混乱的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值