PageRenderTime 15ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/Visual Studio 2008/CSWinFormDataGridView/MultipleLayeredColumnHeader/MainForm.cs

#
C# | 134 lines | 88 code | 21 blank | 25 comment | 5 complexity | 19dd85f7d9258d9d6ad6872e09391ddf MD5 | raw file
  1/************************************* Module Header **************************************\
  2* Module Name:	MultipleLayeredColumnHeader
  3* Project:		CSWinFormDataGridView
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* 
  7* This sample demonstrates how to display multiple layer column headers on the DataGridView.
  8* 
  9* This source is subject to the Microsoft Public License.
 10* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 11* All other rights reserved.
 12* 
 13* History:
 14* * 6/10/2009 3:00 PM Zhi-Xin Ye Created
 15* * 
 16* 
 17\******************************************************************************************/
 18
 19
 20using System;
 21using System.Collections.Generic;
 22using System.ComponentModel;
 23using System.Data;
 24using System.Drawing;
 25using System.Linq;
 26using System.Text;
 27using System.Windows.Forms;
 28
 29namespace CSWinFormDataGridView.MultipleLayeredColumnHeader
 30{
 31    public partial class MainForm : Form
 32    {
 33        public MainForm()
 34        {
 35            InitializeComponent();
 36        }
 37
 38        private void MainForm_Load(object sender, EventArgs e)
 39        {
 40            this.dataGridView1.Columns.Add("JanWin", "Win");
 41            this.dataGridView1.Columns.Add("JanLoss", "Loss");
 42            this.dataGridView1.Columns.Add("FebWin", "Win");
 43            this.dataGridView1.Columns.Add("FebLoss", "Loss");
 44            this.dataGridView1.Columns.Add("MarWin", "Win");
 45            this.dataGridView1.Columns.Add("MarLoss", "Loss");
 46
 47            for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
 48            {
 49                this.dataGridView1.Columns[j].Width = 45;
 50            }
 51
 52            // Enable resizing on the column headers;
 53            this.dataGridView1.ColumnHeadersHeightSizeMode =
 54                 DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
 55
 56            // Adjust the height for the column headers
 57            this.dataGridView1.ColumnHeadersHeight =
 58                        this.dataGridView1.ColumnHeadersHeight * 2;
 59
 60            // Adjust the text alignment on the column headers to make the text display 
 61            // at the center of the bottom;
 62            this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
 63                 DataGridViewContentAlignment.BottomCenter;
 64
 65            // Handle the CellPainting event to draw text for each header cell
 66            this.dataGridView1.CellPainting += new
 67                 DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
 68
 69            // Handle the Paint event to draw "merged" header cells;
 70            this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
 71        }
 72
 73        void dataGridView1_Paint(object sender, PaintEventArgs e)
 74        {
 75            // Data for the "merged" header cells
 76            string[] monthes = { "January", "February", "March" };
 77
 78            for (int j = 0; j < this.dataGridView1.ColumnCount; )
 79            {
 80                // Get the column header cell bounds
 81                Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
 82
 83                r1.X += 1;
 84                r1.Y += 1;
 85                r1.Width = r1.Width * 2 - 2;
 86                r1.Height = r1.Height / 2 - 2;
 87
 88                using (SolidBrush br = 
 89                    new SolidBrush(
 90                        this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor))
 91                {
 92                    e.Graphics.FillRectangle(br, r1);
 93                }
 94
 95                using (Pen p = new Pen(SystemColors.InactiveBorder))
 96                {
 97                    e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom);
 98                }
 99
100                using( StringFormat format = new StringFormat())
101                using (SolidBrush br = 
102                    new SolidBrush(
103                        this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor))
104                {
105                    format.Alignment = StringAlignment.Center;
106                    format.LineAlignment = StringAlignment.Center;
107
108                    e.Graphics.DrawString(monthes[j / 2],
109                        this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
110                        br,
111                        r1,
112                        format);
113                }
114
115                j += 2;
116            }
117        }
118
119        void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
120        {
121            if (e.RowIndex == -1 && e.ColumnIndex > -1)
122            {
123                e.PaintBackground(e.CellBounds, false);
124
125                Rectangle r2 = e.CellBounds;
126                r2.Y += e.CellBounds.Height / 2;
127                r2.Height = e.CellBounds.Height / 2;
128                e.PaintContent(r2);
129
130                e.Handled = true;
131            }
132        }
133    }
134}