winForm datagridview 表头处理

//类名 

  internal class exGridView
    {
        private int cHeight = 0;
        private int cLeft = 0;
        private int cTop = 0;
        private int cWidth = 0;
        public static bool isEnLarged = false;

        public void MergeHeader(object sender, DataGridViewCellPaintingEventArgs e, List<string> colNameCollection, string headerText)
        {
            if (e.RowIndex == -1)
            {
                DataGridView view = sender as DataGridView;
                string name = view.Columns[e.ColumnIndex].Name;
                if (!isEnLarged)
                {
                    view.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
                   
                    view.ColumnHeadersHeight = e.CellBounds.Height * 2;
                    isEnLarged = true;
                }
                if (colNameCollection.Contains(name))
                {
                    Brush brush;
                    Pen pen;
                    Pen pen2;
                    Brush brush2;
                    if (colNameCollection.IndexOf(name) == 0)
                    {
                        this.cTop = e.CellBounds.Top;
                        this.cLeft = e.CellBounds.Left;
                        this.cWidth = e.CellBounds.Width;
                        this.cHeight = e.CellBounds.Height / 2;
                        foreach (string str2 in colNameCollection)
                        {
                            if (!str2.Equals(name))
                            {
                                this.cWidth += view.Columns[str2].Width;
                            }
                        }
                    }
                    Rectangle rect = new Rectangle(this.cLeft, this.cTop, this.cWidth, this.cHeight);
                    using (brush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        e.Graphics.FillRectangle(brush, rect);
                    }
                    using (pen = new Pen(view.GridColor))
                    {
                        e.Graphics.DrawLine(pen, this.cLeft, this.cTop, this.cLeft + this.cWidth, this.cTop);
                        using (pen2 = new Pen(Color.WhiteSmoke))
                        {
                            e.Graphics.DrawLine(pen2, this.cLeft, this.cTop + 1, this.cLeft + this.cWidth, this.cTop + 1);
                            e.Graphics.DrawLine(pen2, this.cLeft, this.cTop + 3, this.cLeft, (this.cTop + this.cHeight) - 2);
                        }
                        e.Graphics.DrawLine(pen, this.cLeft, (this.cTop + this.cHeight) - 1, this.cLeft + this.cWidth, (this.cTop + this.cHeight) - 1);
                        e.Graphics.DrawLine(pen, (this.cLeft + this.cWidth) - 1, this.cTop, (this.cLeft + this.cWidth) - 1, this.cTop + this.cHeight);
                    }
                    if (colNameCollection.IndexOf(name) == 0)
                    {
                        int num = (int) (headerText.Length * e.CellStyle.Font.SizeInPoints);
                        int num3 = ((this.cWidth - num) / 2) - 6;
                        using (brush2 = new SolidBrush(e.CellStyle.ForeColor))
                        {
                            e.Graphics.DrawString(headerText, e.CellStyle.Font, brush2, new PointF((float) (this.cLeft + num3), (float) (this.cTop + 3)));
                        }
                    }
                    int num4 = e.CellBounds.Height / 2;
                    using (brush = new SolidBrush(e.CellStyle.BackColor))
                    {
                        e.Graphics.FillRectangle(brush, new Rectangle(e.CellBounds.X, e.CellBounds.Y + num4, e.CellBounds.Width - 1, (e.CellBounds.Height / 2) - 1));
                    }
                    using (pen = new Pen(view.GridColor))
                    {
                        using (pen2 = new Pen(Color.WhiteSmoke))
                        {
                            e.Graphics.DrawLine(pen2, this.cLeft, (this.cTop + 3) + num4, this.cLeft, ((this.cTop + this.cHeight) - 2) + num4);
                        }
                        e.Graphics.DrawLine(pen, this.cLeft, ((this.cTop + this.cHeight) - 1) + num4, this.cLeft + this.cWidth, ((this.cTop + this.cHeight) - 1) + num4);
                        e.Graphics.DrawLine(pen, (int) ((e.CellBounds.X + e.CellBounds.Width) - 1), (int) (e.CellBounds.Top + num4), (int) ((e.CellBounds.X + e.CellBounds.Width) - 1), (int) ((e.CellBounds.Top + e.CellBounds.Height) + num4));
                    }
                    int num5 = (int) (view.Columns[e.ColumnIndex].HeaderText.Length * e.CellStyle.Font.SizeInPoints);
                    int num7 = (e.CellBounds.Width - num5) / 2;
                    using (brush2 = new SolidBrush(e.CellStyle.ForeColor))
                    {
                        e.Graphics.DrawString(view.Columns[e.ColumnIndex].HeaderText, e.CellStyle.Font, brush2, new PointF((float) (e.CellBounds.X + num7), (float) ((this.cTop + 3) + num4)));
                    }
//                    e.set_Handled(true);
                    e.Handled = true;
                }
            }
        }
   
    }

//datagridview调用

  private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
         exGridView view = new exGridView();
            List<string> colNameCollection = new List<string>();      

            colNameCollection.Add("LRWJH");
            colNameCollection.Add("LRJHWH");
    
            if (((e.ColumnIndex ==7) || (e.ColumnIndex == 8)) || (e.ColumnIndex ==9)))
            {
                view.MergeHeader(sender, e, colNameCollection, "非负荷时间(min)");
            }
            List<string> list2 = new List<string>();
            list2.Add("LRWX");
            list2.Add("LRSCDL");
            list2.Add("LRTS");
            list2.Add("LRSJ");
            list2.Add("LRHX");

            if (((e.ColumnIndex == 15)) || (e.ColumnIndex == 16) || (e.ColumnIndex == 17) ))
            {
                view.MergeHeader(sender, e, list2, "负荷时间(min)");
            }       

       

        }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForms 的 DataGridView 控件中,可以通过使用 ColumnHeader 属性来实现多表头。具体步骤如下: 1. 设置控件的 ColumnHeadersVisible 属性为 false,这将隐藏默认的表头。 2. 添加自定义表头。可以使用 DataGridViewColumn 对象来创建列,然后使用 DataGridView.Columns.Add 方法将列添加到控件中。每个列可以包含一个或多个子列,用于创建自定义表头。 3. 设置每个列的 HeaderCell 属性为 DataGridViewColumnHeaderCell 对象。这将使该列的默认表头与自定义表头分离。 4. 设置每个子列的 HeaderCell 属性为 DataGridViewColumnHeaderCell 对象。这将使该子列的默认表头与自定义表头分离,并将其添加到相应的列中。 下面是一个简单的示例代码,用于创建一个带有两个表头DataGridView 控件: ```csharp // 隐藏默认表头 dataGridView1.ColumnHeadersVisible = false; // 创建第一级表头 DataGridViewColumn column1 = new DataGridViewTextBoxColumn(); column1.HeaderText = "表头1"; column1.Name = "Column1"; dataGridView1.Columns.Add(column1); // 创建第二级表头 DataGridViewColumn subColumn1 = new DataGridViewTextBoxColumn(); subColumn1.HeaderText = "子表头1"; subColumn1.Name = "SubColumn1"; DataGridViewColumn subColumn2 = new DataGridViewTextBoxColumn(); subColumn2.HeaderText = "子表头2"; subColumn2.Name = "SubColumn2"; column1.HeaderCell = new DataGridViewColumnHeaderCell(); column1.HeaderCell.Value = "表头1"; column1.HeaderCell.Style.Font = new Font(dataGridView1.Font, FontStyle.Bold); column1.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter; column1.HeaderCell.Style.BackColor = Color.LightGray; column1.HeaderCell.Style.WrapMode = DataGridViewTriState.True; column1.HeaderCell.Style.Padding = new Padding(2, 2, 2, 2); column1.HeaderCell.Style.SelectionBackColor = Color.DarkGray; column1.HeaderCell.Style.SelectionForeColor = Color.White; column1.HeaderCell.Style.SelectionBorderWidth = 2; column1.HeaderCell.Style.SelectionBorderColor = Color.Black; column1.HeaderCell.Style.SelectionFont = new Font(dataGridView1.Font, FontStyle.Bold); column1.HeaderCell.Style.SelectionAlignment = DataGridViewContentAlignment.MiddleCenter; subColumn1.HeaderCell = new DataGridViewColumnHeaderCell(); subColumn1.HeaderCell.Value = "子表头1"; subColumn2.HeaderCell = new DataGridViewColumnHeaderCell(); subColumn2.HeaderCell.Value = "子表头2"; column1.DataGridView.Columns.AddRange(new DataGridViewColumn[] { subColumn1, subColumn2 }); ``` 在这个示例中,我们创建了一个带有两个表头DataGridView 控件,并将其添加到窗体上。第一级表头为“表头1”,第二级表头包含两个子列:“子表头1”和“子表头2”。我们还设置了一些样式属性,以更改表头的外观和行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值