惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Martin Fowler
Martin Fowler
B
Blog RSS Feed
D
DataBreaches.Net
L
LangChain Blog
月光博客
月光博客
S
SegmentFault 最新的问题
阮一峰的网络日志
阮一峰的网络日志
V
Visual Studio Blog
美团技术团队
Jina AI
Jina AI
博客园 - 司徒正美
雷峰网
雷峰网
Last Week in AI
Last Week in AI
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
小众软件
小众软件
罗磊的独立博客
博客园_首页
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
A
About on SuperTechFans
Engineering at Meta
Engineering at Meta

博客园 - hugh-lin

MySql数据库、表名大小写的问题 [转]Spring--简单使用quartz实现定时作业 [转]PowerDesigner使用技巧 IBatis的自动生成工具abator的改造 - hugh-lin - 博客园 Struts2.1开发笔记-2 Struts2.1开发笔记-1 PowerDesigner 12.5 导致 Word 2003鼠标没反应的解决方法 J2EE开发之常用开源项目介绍(转) [转]通过ScriptManager在客户端来调用服务器端方法或者webService的方法 - hugh-lin - 博客园 [转]关闭Visual Studio 实时调试器 [转]java安装配置 [转]文件缓存的方式减少网站负载 [转]C#中得到程序当前工作目录和执行目录的一些方法 [转] NHibernate对像版本控制使用示例 [转]Web项目下NHibernate的Session管理的解决方案 分页 oracle 导入/导出 [转]使用.NET实现断点续传 [转]IIS站点管理类 [转]C#的usb通讯编程
[转]Datagridview 实现二维表头
hugh-lin · 2007-10-26 · via 博客园 - hugh-lin

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

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

  1,继承DataGridView,添加表头信息类。
  2,添加CellPainting,代码如下:

  private void DataGridViewEx_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        
{
            
if (e.RowIndex == -1)
            
{
             
//   int w = dataGridView1.HorizontalScrollingOffset + dataGridView1.TopLeftHeaderCell.Size.Width + dataGridView1.Columns[0].Width + 10;


                Rectangle newRect 
= new Rectangle(e.CellBounds.X + 1,
               e.CellBounds.Y 
+ 1, e.CellBounds.Width - 4,
               e.CellBounds.Height 
- 4);

                
using (
                    Brush gridBrush 
= new SolidBrush(this.GridColor),
                    backColorBrush 
= new SolidBrush(e.CellStyle.BackColor))
                
{
                    
using (Pen gridLinePen = new Pen(gridBrush))
                    
{
                        
// Erase the cell.
                        e.Graphics.FillRectangle(backColorBrush, e.CellBounds);

                        
// Draw the grid lines (only the right and bottom lines;
                        
// DataGridView takes care of the others).
                        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.Right 
- 1,
                                e.CellBounds.Top 
+ e.ClipBounds.Height / 2, e.CellBounds.Right - 1,
                                e.CellBounds.Bottom);
                        }

                        
else
                        
{
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right 
- 1,
                                                          e.CellBounds.Top, e.CellBounds.Right 
- 1,
                                                          e.CellBounds.Bottom);
                        }


                        
// Draw the inset highlight box.
                        
//   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.Left, e.CellBounds.Bottom 
- e.CellBounds.Height / 2, e.CellBounds.Right, e.CellBounds.Bottom - e.CellBounds.Height / 2);
                        }

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

                      

                        
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)
                    
                        
{
                            Rectangle recCell 
= new Rectangle(e.CellBounds.X - 1 - topRow.Cells[e.ColumnIndex].SpanRowWith,
           e.CellBounds.Y 
+ 1, topRow.Cells[e.ColumnIndex].SpanRowWith,
           e.CellBounds.Height 
/ 2);


                            StringFormat sf 
= new StringFormat();

                            sf.Alignment 
= StringAlignment.Center;


                            e.Graphics.DrawString(topRow.Cells[e.ColumnIndex].Text, e.CellStyle.Font, 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,效果图

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