background image

 最近把我们的 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,