实现DataGridView的多层表头

因工作需要使用DataGridView实现多层表头的显示,在网上找了些实现的例子(在此感谢manimanihome的博文提供原始素材,需要看原理的请转过去查看),但在绘制时的计算逻辑上有些问题,自己经过了一些修改勉强可以满足需要了。

还存在的问题有:在拖动改变列宽后有时候字/线没有刷过来;鼠标在标题栏上移动时会不停的刷影响效果。

使用时需要注意3个地方:主要类包含MultiHead,MutiHeadDGV:DataGridView;

Datagridview的ColumnHeadersHeightSizeMode属性最好设为EnableResizing;

布局的列数需要与多层列的最低级标题数一致。

下面上代码:

MultiHead类

    class MultiHead
    {
        #region ...静态...
        private static SortedList<string, int> cellPointX;

        /// <summary>
        /// 获取标题单元格的左边距
        /// </summary>
        /// <param name="rowindex"></param>
        /// <param name="columnindex"></param>
        /// <returns></returns>
        private static int GetCellPointX(int rowindex, int columnindex)
        {
            string key = string.Format("{0}.{1}",rowindex,columnindex);
            return cellPointX[key];
        }

        /// <summary>
        /// 设置或更新标题单元格的左边距
        /// </summary>
        /// <param name="rowindex"></param>
        /// <param name="columnindex"></param>
        /// <param name="x"></param>
        /// <returns></returns>
        private static void SetCellPoint(int rowindex, int columnindex, int x)
        {
            if (cellPointX == null)
                cellPointX = new SortedList<string, int>();

            string key = string.Format("{0}.{1}", rowindex, columnindex);
            if (cellPointX.ContainsKey(key))
                cellPointX[key] = x;
            else
                cellPointX.Add(key, x);
        }
        #endregion

        private DataGridView dataGridView;

        public MultiHead(DataGridView grid)
        {
            this.dataGridView = grid;
            string title = "";
            for (int i = 0; i != this.dataGridView.Columns.Count - 1; ++i)
            {
                title += this.dataGridView.Columns[i].HeaderText + ",";
            }
            title = title.Substring(1, title.Length - 2);
            this.titleHead = new string[] { title };

        }
        //通過构造函數來限制title的格式,始終与grid保持一致
        public MultiHead(DataGridView grid, string[] title)
        {
            //grid不等于null
            for (int i = 0; i != title.Length - 1; ++i)
            {
                string[] s = title[i].Split(',');
                if (grid.Columns.Count == s.Length)
                {
                    continue;
                }
                else
                {
                    throw new Exception("title的元素個數与grid的欄位總數不一致.");
                }
            }
            this.dataGridView = grid;
            this.titleHead = title;
        }

        private string[] titleHead;
        public string[] TitleHead
        {
            get
            {
                return titleHead;
            }
        }

        public string Draw(DataGridViewCellPaintingEventArgs e)
        {
            string message = string.Empty;
            if (e.RowIndex == -1)
            {
                using (
                    Brush gridBrush = new SolidBrush(this.dataGridView.GridColor),
                    backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                {
                    using (Pen gridLinePen = new Pen(gridBrush))
                    {
                        
                        gridLinePen.Color = dataGridView.GridColor;
                        if (e.ColumnIndex == -1)
                        {
                            e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                            //畫右邊線
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                            //畫bottom線
                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right, e.CellBounds.Bottom - 1);
                        }
                        else
                        {

                            //第一行已经绘制的上一个标题
                            string[] drawtitles = new string[titleHead.Length];

                            for (int i = 0; i < titleHead.Length; ++i)//逐行畫
                            {
                                SetCellPoint(i, e.ColumnIndex,e.CellBounds.Width);

                                int width;
                                int height;
                                int locationX;
                                int locationY;
                                int paintingwidth=0;
                                
                                string[] currRow = titleHead[i].Split(',');

                                width = e.CellBounds.Width;
                                //開始列
                                int startColIndex = e.ColumnIndex;
                                while (startColIndex > 0)
                                {
                                    if (currRow[e.ColumnIndex] == currRow[startColIndex - 1])
                                    {
                                        if (this.dataGridView.Columns[startColIndex - 1].Visible)
                                        {
                                            width += this.dataGridView.Columns[startColIndex - 1].Width;
                                        }

                                        if (startColIndex <= e.ColumnIndex)
                                            paintingwidth += this.dataGridView.Columns[startColIndex].Width;

                                        startColIndex--;
                                    }
                                    else
                                    {
                                        
                                        break;
                                    }
                                    
                                }
                                paintingwidth += GetCellPointX(i, startColIndex);
                                //結束列
                                int endColIndex = e.ColumnIndex;
                                

                                while (endColIndex < this.dataGridView.Columns.Count - 1)
                                {
                                    if (currRow[e.ColumnIndex] == currRow[endColIndex + 1])
                                    {
                                        if (this.dataGridView.Columns[endColIndex + 1].Visible)
                                        {
                                            width += this.dataGridView.Columns[endColIndex + 1].Width;
                                        
                                        }
                                        endColIndex++;
                                    }
                                    else
                                    {

                                        break;
                                    }
                                }

                                height = e.CellBounds.Height / titleHead.Length;
                                //開始行
                                int startRowIndex = i;
                                while (startRowIndex > 0)
                                {
                                    string[] overRow = titleHead[startRowIndex - 1].Split(',');
                                    if (currRow[e.ColumnIndex] == overRow[e.ColumnIndex])
                                    {
                                        int overStartColIndex = e.ColumnIndex;
                                        int overEndColIndex = e.ColumnIndex;

                                        while (overStartColIndex > 0)
                                        {
                                            if (overRow[e.ColumnIndex] == currRow[overStartColIndex - 1])
                                            {
                                                overStartColIndex--;
                                            }
                                            else
                                            {
                                                break;
                                            }

                                        }
                                        while (overEndColIndex < this.dataGridView.Columns.Count - 1)
                                        {
                                            if (overRow[e.ColumnIndex] == overRow[overEndColIndex + 1])
                                            {
                                                overEndColIndex++;
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }

                                        if (startColIndex == overStartColIndex && endColIndex == overEndColIndex)
                                        {

                                            height += e.CellBounds.Height / titleHead.Length;
                                            startRowIndex--;
                                        }
                                        else
                                        {
                                            break;
                                        }


                                    }
                                    else
                                    {
                                        break;
                                    }
                                }

                                //結束行
                                int endRowIndex = i;
                                while (endRowIndex < titleHead.Length - 1)
                                {

                                    string[] lowRow = titleHead[endRowIndex + 1].Split(',');
                                    if (currRow[e.ColumnIndex] == lowRow[e.ColumnIndex])
                                    {
                                        int lowStartColIndex = e.ColumnIndex;
                                        int lowEndColIndex = e.ColumnIndex;

                                        while (lowStartColIndex > 0)
                                        {
                                            if (lowRow[e.ColumnIndex] == currRow[lowStartColIndex - 1])
                                            {
                                                lowStartColIndex--;
                                            }
                                            else
                                            {
                                                break;
                                            }

                                        }
                                        while (lowEndColIndex < this.dataGridView.Columns.Count - 1)
                                        {
                                            if (lowRow[e.ColumnIndex] == lowRow[lowEndColIndex + 1])
                                            {
                                                lowEndColIndex++;
                                            }
                                            else
                                            {
                                                break;
                                            }
                                        }

                                        if (startColIndex == lowStartColIndex && endColIndex == lowEndColIndex)
                                        {
                                            height += e.CellBounds.Height / titleHead.Length;
                                            endRowIndex++;
                                        }
                                        else
                                        {
                                            break;
                                        }

                                    }
                                    else
                                    {
                                        break;
                                    }

                                }

                                locationX = e.CellBounds.Right - paintingwidth;//  e.CellBounds.Right - width;
                                locationY = e.CellBounds.Bottom - (titleHead.Length - endRowIndex - 1) * e.CellBounds.Height / titleHead.Length - height;
                                

                                Rectangle recCell = new Rectangle(locationX, locationY, width, height);
                                message += string.Format("{0}:({1},{2},{3},{4})\r\n", currRow[e.ColumnIndex], locationX, locationY, width, height);
                                //erase cell
                                e.Graphics.FillRectangle(backColorBrush, recCell);

                                //畫right和bottom線
                                e.Graphics.DrawLine(gridLinePen, locationX + width - 1, locationY - 1, locationX + width - 1, locationY + height - 1);
                                e.Graphics.DrawLine(gridLinePen, locationX - 1, locationY + height - 1, locationX + width - 1, locationY + height - 1);
                                
                                //畫文字
                                StringFormat sf = new StringFormat();
                                sf.Alignment = StringAlignment.Center;
                                sf.LineAlignment = StringAlignment.Center;
                                e.Graphics.DrawString(currRow[e.ColumnIndex], e.CellStyle.Font, Brushes.Black, recCell, sf);


                            }
                        }

                        e.Handled = true;


                    }
                }
            }
            return message;
        }
    }


MutiHeadDGV:DataGridView


    class MutiHeadDGV:DataGridView
    {
        private string[] titleHead;
        public string[] TitleHead
        {
            get
            {
                return this.titleHead;
            }
            set
            {
                this.titleHead = value;
            }
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            titleHead = new string[] { "基础资料,基础资料,基础资料,基础资料,附加信息,附加信息,附加信息,附加信息,描述",
                "姓名,籍贯,出生时间,性别,现住址,职业,收入,爱好,描述"};

                
            MultiHead m = new MultiHead(this, titleHead);

        }

    }

TitleHead属性<strong style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">多层标题是可以在外部赋值的哦</strong>



### 回答1: datagridview表头是用来显示列名的,默认情况下是可以看到的。如果你需要禁止表头的显示,可以通过以下步骤实现: 1. 打开窗体的设计视图,在DataGridView控件的属性窗口中找到Columns属性。 2. 单击Columns属性旁边的“…”按钮,打开列编辑器。 3. 在编辑器中,为每一列设置HeaderText属性为相应的列名,这样在表头处就会显示列名。 4. 在DataGridView的属性窗口中,找到ColumnHeadersVisible属性,将其值设置为False,这样表头就被隐藏起来了。 这样做之后,你将看到在运行时表头不再显示,只有数据行可见。请注意,这只是隐藏了表头的显示,并不会删除或者改变表头的结构,因此你仍然可以通过代码或者其他方式来操作和访问表头。 ### 回答2: 要禁止 DataGridView表头,可以使用以下方法: 1. 设置 DataGridView 的 ColumnHeadersVisible 属性为 False。这将隐藏整个表头,并且禁用了对表头的排序、拖动列等操作。代码示例: DataGridView1.ColumnHeadersVisible = False 2. 如果你只想禁止用户拖动列而保留表头的显示和其他操作,可以使用 ColumnHeadersHeightSizeMode 属性来禁用拖动功能。代码示例: DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing 3. 如果你希望保留表头的显示,但要禁用所有的表头操作,可以使用 HeaderCell 属性来禁用鼠标事件并移除掉所有的 ContextMenuStrip。代码示例: For Each col As DataGridViewColumn In DataGridView1.Columns col.HeaderCell = New DataGridViewColumnHeaderCell() With { .Value = col.HeaderText, .ToolTipText = col.HeaderText, .ContextMenuStrip = Nothing } Next 无论使用哪种方法,都可以有效地禁止 DataGridView表头操作。根据具体的需求,选择适合的方法即可。 ### 回答3: DataGridView是.NET Framework中的一个控件,用于展示和编辑数据。如果要禁用DataGridView表头,可以使用以下方法: 1. 设置DataGridView的ColumnHeadersVisible属性为False。这将隐藏掉整个表头部分,包括列名和排序按钮。 2. 使用样式设置隐藏表头。首先,可以通过设置DataGridView的EnableHeadersVisualStyles属性为False,禁用默认的表头样式。然后,可以通过修改DataGridView的DefaultCellStyle属性来隐藏表头的外观,比如将表头的字体颜色设置为与背景颜色相同。 3. 使用尺寸调整行隐藏表头。在DataGridView的RowHeadersVisible属性为False的情况下,可以通过将表头行的高度设置为0,来隐藏掉表头。这样表头行将不可见,但是单元格内容仍然可以正常显示和编辑。 总结起来,禁止DataGridView表头可以通过隐藏整个表头、修改样式或调整行高度等方式来实现。具体选择哪种方式取决于实际需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值