PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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. using System;
  19. using System.Collections.Generic;
  20. using System.ComponentModel;
  21. using System.Data;
  22. using System.Drawing;
  23. using System.Linq;
  24. using System.Text;
  25. using System.Windows.Forms;
  26. namespace CSWinFormDataGridView.MultipleLayeredColumnHeader
  27. {
  28. public partial class MainForm : Form
  29. {
  30. public MainForm()
  31. {
  32. InitializeComponent();
  33. }
  34. private void MainForm_Load(object sender, EventArgs e)
  35. {
  36. this.dataGridView1.Columns.Add("JanWin", "Win");
  37. this.dataGridView1.Columns.Add("JanLoss", "Loss");
  38. this.dataGridView1.Columns.Add("FebWin", "Win");
  39. this.dataGridView1.Columns.Add("FebLoss", "Loss");
  40. this.dataGridView1.Columns.Add("MarWin", "Win");
  41. this.dataGridView1.Columns.Add("MarLoss", "Loss");
  42. for (int j = 0; j < this.dataGridView1.ColumnCount; j++)
  43. {
  44. this.dataGridView1.Columns[j].Width = 45;
  45. }
  46. // Enable resizing on the column headers;
  47. this.dataGridView1.ColumnHeadersHeightSizeMode =
  48. DataGridViewColumnHeadersHeightSizeMode.EnableResizing;
  49. // Adjust the height for the column headers
  50. this.dataGridView1.ColumnHeadersHeight =
  51. this.dataGridView1.ColumnHeadersHeight * 2;
  52. // Adjust the text alignment on the column headers to make the text display
  53. // at the center of the bottom;
  54. this.dataGridView1.ColumnHeadersDefaultCellStyle.Alignment =
  55. DataGridViewContentAlignment.BottomCenter;
  56. // Handle the CellPainting event to draw text for each header cell
  57. this.dataGridView1.CellPainting += new
  58. DataGridViewCellPaintingEventHandler(dataGridView1_CellPainting);
  59. // Handle the Paint event to draw "merged" header cells;
  60. this.dataGridView1.Paint += new PaintEventHandler(dataGridView1_Paint);
  61. }
  62. void dataGridView1_Paint(object sender, PaintEventArgs e)
  63. {
  64. // Data for the "merged" header cells
  65. string[] monthes = { "January", "February", "March" };
  66. for (int j = 0; j < this.dataGridView1.ColumnCount; )
  67. {
  68. // Get the column header cell bounds
  69. Rectangle r1 = this.dataGridView1.GetCellDisplayRectangle(j, -1, true);
  70. r1.X += 1;
  71. r1.Y += 1;
  72. r1.Width = r1.Width * 2 - 2;
  73. r1.Height = r1.Height / 2 - 2;
  74. using (SolidBrush br =
  75. new SolidBrush(
  76. this.dataGridView1.ColumnHeadersDefaultCellStyle.BackColor))
  77. {
  78. e.Graphics.FillRectangle(br, r1);
  79. }
  80. using (Pen p = new Pen(SystemColors.InactiveBorder))
  81. {
  82. e.Graphics.DrawLine(p, r1.X, r1.Bottom, r1.Right, r1.Bottom);
  83. }
  84. using( StringFormat format = new StringFormat())
  85. using (SolidBrush br =
  86. new SolidBrush(
  87. this.dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor))
  88. {
  89. format.Alignment = StringAlignment.Center;
  90. format.LineAlignment = StringAlignment.Center;
  91. e.Graphics.DrawString(monthes[j / 2],
  92. this.dataGridView1.ColumnHeadersDefaultCellStyle.Font,
  93. br,
  94. r1,
  95. format);
  96. }
  97. j += 2;
  98. }
  99. }
  100. void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
  101. {
  102. if (e.RowIndex == -1 && e.ColumnIndex > -1)
  103. {
  104. e.PaintBackground(e.CellBounds, false);
  105. Rectangle r2 = e.CellBounds;
  106. r2.Y += e.CellBounds.Height / 2;
  107. r2.Height = e.CellBounds.Height / 2;
  108. e.PaintContent(r2);
  109. e.Handled = true;
  110. }
  111. }
  112. }
  113. }