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 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 colNameCollection = new List();      

            colNameCollection.Add("LRWJH");
            colNameCollection.Add("LRJHWH");
    
            if (((e.ColumnIndex ==7) || (e.ColumnIndex == 8)) || (e.ColumnIndex ==9)))
            {
                view.MergeHeader(sender, e, colNameCollection, "非负荷时间(min)");
            }
            List list2 = new List();
            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)");
            }       

       

        }

 

你可能感兴趣的:(C#,winform,list,float,string,object,class)