Click here to Skip to main content
15,892,072 members
Articles / Desktop Programming / Windows Forms

DataGrid with Sizable Multiline Column Headers

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
8 Mar 20063 min read 189K   1.9K   59  
Allows user defined sizing of DataGrid column header height to support multi-line header text.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace SizableColumnHeaderDataGridSample
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{


	#region Not important sample stuff

		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		private ThreeRiversTechnologies.SizableColumnHeaderDataGrid sizableColumnHeaderDataGrid1;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.sizableColumnHeaderDataGrid1 = new ThreeRiversTechnologies.SizableColumnHeaderDataGrid();
			((System.ComponentModel.ISupportInitialize)(this.sizableColumnHeaderDataGrid1)).BeginInit();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.textBox1.Location = new System.Drawing.Point(136, 264);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(56, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "";
			// 
			// label1
			// 
			this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.label1.Location = new System.Drawing.Point(16, 264);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(128, 23);
			this.label1.TabIndex = 2;
			this.label1.Text = "Column Header Height:";
			this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// button1
			// 
			this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.button1.Location = new System.Drawing.Point(200, 264);
			this.button1.Name = "button1";
			this.button1.TabIndex = 3;
			this.button1.Text = "Apply";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// sizableColumnHeaderDataGrid1
			// 
			this.sizableColumnHeaderDataGrid1.CaptionVisible = false;
			this.sizableColumnHeaderDataGrid1.ColumnHeaderHeight = 39;
			this.sizableColumnHeaderDataGrid1.DataMember = "";
			this.sizableColumnHeaderDataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.sizableColumnHeaderDataGrid1.Location = new System.Drawing.Point(8, 16);
			this.sizableColumnHeaderDataGrid1.Name = "sizableColumnHeaderDataGrid1";
			this.sizableColumnHeaderDataGrid1.Size = new System.Drawing.Size(344, 240);
			this.sizableColumnHeaderDataGrid1.TabIndex = 4;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(360, 293);
			this.Controls.Add(this.sizableColumnHeaderDataGrid1);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.label1);
			this.Name = "Form1";
			this.Text = "Form1";
			this.Load += new System.EventHandler(this.Form1_Load);
			((System.ComponentModel.ISupportInitialize)(this.sizableColumnHeaderDataGrid1)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}
	
	#endregion // Not important sample stuff



	#region Sample code
	
		private void Form1_Load(object sender, System.EventArgs e)
		{
			//
			// Build tablestyle
			//
			DataGridTableStyle ts = new DataGridTableStyle();

			DataGridTextBoxColumn col1 = new DataGridTextBoxColumn();
			col1.MappingName = "one";
			col1.HeaderText = "Row Index\nmulitplied\nby 1";
			ts.GridColumnStyles.Add(col1);
			
			DataGridTextBoxColumn col2 = new DataGridTextBoxColumn();
			col2.MappingName = "two";
			col2.HeaderText = "Row Index\nmulitplied\nby 2";
			ts.GridColumnStyles.Add(col2);
			
			DataGridTextBoxColumn col3 = new DataGridTextBoxColumn();
			col3.MappingName = "three";
			col3.HeaderText = "Row Index\nmulitplied\nby 3";
			ts.GridColumnStyles.Add(col3);
			
			sizableColumnHeaderDataGrid1.TableStyles.Clear();
			sizableColumnHeaderDataGrid1.TableStyles.Add(ts);


			//
			// Build sample data
			//
			DataTable dt = new DataTable();
			dt.Columns.Add(new DataColumn("one"));
			dt.Columns.Add(new DataColumn("two"));
			dt.Columns.Add(new DataColumn("three"));
			DataRow row;

			for (Int32 i = 0; i < 100; i++)
			{
				row = dt.NewRow();
				row["one"] = i;
				row["two"] = i * 2;
				row["three"] = i * 3;
				dt.Rows.Add(row);

			}

			
			
			//
			// Bind
			//
			sizableColumnHeaderDataGrid1.DataSource = dt;

			//
			// Show current column header size
			//
			textBox1.Text = sizableColumnHeaderDataGrid1.ColumnHeaderHeight.ToString();

		}

	
		private void button1_Click(object sender, System.EventArgs e)
		{
			//
			// Apply new column header size
			//
			sizableColumnHeaderDataGrid1.ColumnHeaderHeight = Convert.ToInt32(textBox1.Text);

			//
			// Must call invalidate in order to force datagrid to repaint
			//
			sizableColumnHeaderDataGrid1.Invalidate();

		}

	
	#endregion // Sample code



	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
Doug develops software for Three Rivers Technologies (www.3riverstech.com) and their clients.

Comments and Discussions