我要做一个显示工资表的。   需要双层表头。 初学者,不会搞,看了些例子,但都不完善,有的太复杂看不太懂。
  哪位可以给一个简单易用,适合初学者的  例子。  感谢啦! 希望能够详细的。
奉上全部积分。  
如果发帖不方便,请发我邮箱([email protected])。 谢谢啦!!

解决方案 »

  1.   

    DatagridView 本身不能实现这个功能,除非你在它基础上写个派生类!另外DEV的GridControl控件可以实现这个功能,用起来有点复杂
      

  2.   

    网上可以找到,override onpaint()
      

  3.   

    DEV的GridControl 中的AdvBandedGridView
      

  4.   

    DEV是c++的把。  我用的c#做的。  
      

  5.   

    楼主,Dev是net的第三方控件。
      

  6.   

    我自己写了一个,可以告诉你基本原理和步骤,但是太详细的就不知道该怎么说了。
    原理:重写 DatagridviewPainting.表头有移动的时候,Refresh().
    步骤: 1、确定你要分层的的列
          2、把列之间的竖线去掉。
          3、在这些列表头的中间横向画一条分割线
          4、再给每列的右侧分别画竖线。
          5、计算双层表头各个字符串的位置,写出来。
          6、其中用到的技术细节请查看 MSDN.
         7、计算字符串位置的时候要看仔细了,不然就乱了。
    这些问题不难,就是很麻烦。不怕麻烦就难做出来。
      

  7.   

    VS2005的DataGridView 多维合并标题 功能拓展
      

  8.   

    上面是vb的,有c#的代码
      

  9.   

    这个网上例子很多啊~
    google一下一大把!
      

  10.   

    多表头
    http://www.lwolf.cn/blog/article/code/322.htm
      

  11.   

    dev是devexpress控件,是.NET第三方控件,当中包含的控件非常之多
      

  12.   

    这个网址你可以参考参考,里面有很多例子和源代码
    对于datagridview实现各种效果的源代码
    http://www.cnblogs.com/peterzb/archive/2009/05/29/1491891.html
    另外还有其他控件的例子,希望对你有帮助!
      

  13.   

    我自己找了个简单的 重绘。 功能可能不太全。int top = 0;
            int left = 0;
            int height = 0;
            int width1 = 0;
            private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                #region 重绘datagridview表头
                DataGridView dgv = (DataGridView)(sender);
                if (e.RowIndex == -1 && (e.ColumnIndex == 18 || e.ColumnIndex == 19)) //在第三和第四列上加了个表头
                {                //e.CellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
                    //e.CellStyle.WrapMode = DataGridViewTriState.True;
                    if (e.ColumnIndex == 18)
                    {
                        top = e.CellBounds.Top;
                        left = e.CellBounds.Left;
                        height = e.CellBounds.Height;
                        width1 = e.CellBounds.Width;
                    }
                    int width2 = this.dataGridView1.Columns[19].Width;
                                   Rectangle rect = new Rectangle(left, top, width1 + width2 , e.CellBounds.Height);
                    using (Brush backColorBrush = new SolidBrush(Color.White))
                    {
                        //抹去原来的cell背景
                        e.Graphics.FillRectangle(backColorBrush, rect);
                    }                using (Pen gridLinePen = new Pen(dgv.GridColor))
                    {
                        e.Graphics.DrawLine(gridLinePen, left, top, left + width1 + width2, top);
                        e.Graphics.DrawLine(gridLinePen, left, top + height / 2, left + width1 + width2, top + height / 2); // 中间线
                        e.Graphics.DrawLine(gridLinePen, left + width1, top + height / 2, left + width1, top + height);// 短竖线
                        e.Graphics.DrawLine(gridLinePen, (left + width1+width2 )*999/1000, top , (left + width1+width2)*999/1000 , top + height);
                        e.Graphics.DrawLine(gridLinePen, left, top + height* 99/100, left + width1 + width2, top + height* 99/100); 
                        //计算绘制字符串的位置
                        string columnValue = "扣费";
                        SizeF sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                        float lstr = (width1 + width2 - sf.Width) / 2;
                        float rstr = (height / 2 - sf.Height) / 2;
                        //画出文本框                    if (columnValue != "")
                        {
                            e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                       new SolidBrush(e.CellStyle.ForeColor),
                                                         left + lstr,
                                                         top + rstr,
                                                         StringFormat.GenericDefault);
                        }
                        //计算绘制字符串的位置
                        columnValue = "餐费";
                        sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                        lstr = (width1 - sf.Width) / 2;
                        rstr = (height / 2 - sf.Height) / 2;
                        //画出文本框                    if (columnValue != "")
                        {
                            e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                       new SolidBrush(e.CellStyle.ForeColor),
                                                         left + lstr,
                                                         top + height / 2 + rstr,
                                                         StringFormat.GenericDefault);
                        }
                        //计算绘制字符串的位置
                        columnValue = "保险";
                        sf = e.Graphics.MeasureString(columnValue, e.CellStyle.Font);
                        lstr = (width2 - sf.Width) / 2;
                        rstr = (height / 2 - sf.Height) / 2;
                        //画出文本框                    if (columnValue != "")
                        {
                            e.Graphics.DrawString(columnValue, e.CellStyle.Font,
                                                       new SolidBrush(e.CellStyle.ForeColor),
                                                         left + width1 + lstr,
                                                         top + height / 2 + rstr,
                                                         StringFormat.GenericDefault);
                        }                }
                    e.Handled = true;
                }