zoukankan      html  css  js  c++  java
  • DataGridView合并表头实现

    在网上找到这段代码先收藏一下。自己要显示的效果

    也是根据下面一段代码改变过来的。所以标记一下。

    二维表头的实现

           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 == 3 || e.ColumnIndex == 4))
                {

                    //e.CellStyle.Font = new Font(dataGridView1.DefaultCellStyle.Font, FontStyle.Bold);
                    //e.CellStyle.WrapMode = DataGridViewTriState.True;
                    if (e.ColumnIndex == 3)
                    {
                        top = e.CellBounds.Top;
                        left = e.CellBounds.Left;
                        height = e.CellBounds.Height;
                        width1 = e.CellBounds.Width;
                    }


                    int width2 = this.dataGridView1.Columns[4].Width;

                    Rectangle rect = new Rectangle(left, top, width1 + width2, e.CellBounds.Height);
                    using (Brush backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        //抹去原来的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);

                        //计算绘制字符串的位置
                        string columnValue = "Year";
                        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;


                }
                #endregion
            }

  • 相关阅读:
    jQuery对象初始化的多种传参数形式
    Ajax核心技术之XMLHttpRequest
    【转载】神奇的css属性pointer-events
    JavaScript学习笔记——错误处理
    jquery validate的漂亮css样式验证
    JavaScript Math和Number对象研究
    《JavaScript语言精粹》笔记
    HTML5版的String Avoider小游戏
    IE11 F12工具报错
    css之图片羽化处理
  • 原文地址:https://www.cnblogs.com/shunliy/p/1323056.html
Copyright © 2011-2022 走看看