Click here to Skip to main content
15,886,026 members
Articles / Programming Languages / C#

SourceGrid - Open Source C# Grid Control

Rate me:
Please Sign up or sign in to vote.
4.94/5 (429 votes)
4 Aug 2013MIT24 min read 4.8M   23.7K   1K  
SourceGrid is a free open source grid control. Supports virtual grid, custom cells and editors, advanced formatting options and many others features
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace SourceGrid
{
	public class GridWorksheet : Grid
	{
		private System.ComponentModel.IContainer components = null;

		public GridWorksheet()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

			m_iImageAddColumn = AddMenuImage(GridImage.InsertCol);
			m_iImageAddRow = AddMenuImage(GridImage.InsertRow);
			m_iImageRemoveColumn = AddMenuImage(GridImage.DeleteCol);
			m_iImageRemoveRow = AddMenuImage(GridImage.DeleteRow);

			FixedCols = 1;
			FixedRows = 1;

			EnableRowColSpan = false;

			Redim(36,36);
		}

		/// <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 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.SuspendLayout();
			// 
			// GridWorksheet
			// 
			this.Name = "GridWorksheet";
			this.Size = new System.Drawing.Size(200, 200);
			this.ResumeLayout(false);

		}
		#endregion

		//image for menu
		private int m_iImageAddColumn;
		private int m_iImageAddRow;
		private int m_iImageRemoveColumn;
		private int m_iImageRemoveRow;

		protected Cell CreateCaptionTopCell()
		{
			Cell tmp = new CellHeaderWorksheetRow();
			tmp.TextAlignment = ContentAlignment.MiddleCenter;

			MenuItem l_mnAddCol = new MenuItem("Add Column", new EventHandler(ContextMenu_AddColumn));
			SetMenuImage(l_mnAddCol,m_iImageAddColumn);
			MenuItem l_mnRemoveCol = new MenuItem("Remove Column", new EventHandler(ContextMenu_RemoveColumn));
			SetMenuImage(l_mnRemoveCol,m_iImageRemoveColumn);
			tmp.ContextMenuItems.Add(l_mnAddCol);
			tmp.ContextMenuItems.Add(l_mnRemoveCol);

			return tmp;
		}
		protected Cell CreateCaptionLeftCell()
		{
			Cell tmp = new CellHeaderWorksheetCol();
			tmp.TextAlignment = ContentAlignment.MiddleCenter;

			MenuItem l_mnAddRow = new MenuItem("Add Row", new EventHandler(ContextMenu_AddRow));
			SetMenuImage(l_mnAddRow,m_iImageAddRow);
			MenuItem l_mnRemoveRow = new MenuItem("Remove Row", new EventHandler(ContextMenu_RemoveRow));
			SetMenuImage(l_mnRemoveRow,m_iImageRemoveRow);
			tmp.ContextMenuItems.Add(l_mnAddRow);
			tmp.ContextMenuItems.Add(l_mnRemoveRow);

			return tmp;
		}

		protected void ContextMenu_AddRow(object sender, EventArgs e)
		{
			if (FocusCell != null)
				AddRow(FocusCell.Row);
		}
		protected void ContextMenu_RemoveRow(object sender, EventArgs e)
		{
			if (FocusCell != null)
				RemoveRow(FocusCell.Row);
		}
		protected void ContextMenu_AddColumn(object sender, EventArgs e)
		{
			if (FocusCell != null)
				AddColumn(FocusCell.Col);
		}
		protected void ContextMenu_RemoveColumn(object sender, EventArgs e)
		{
			if (FocusCell != null)
				RemoveColumn(FocusCell.Col);
		}

		protected Cell CreateNormalCell()
		{
			Cell tmp = new CellWorksheet();
			return tmp;
		}

		public static string GetColCaption(int p_Col)
		{
			int l_NumLap = ((p_Col-1) / 26);
			int l_Remainder = (p_Col) - (l_NumLap * 26);
			string l_tmp = "";
			//for (int i = 0; i < l_NumLap; i++)
			if (l_NumLap>0)
				l_tmp += GetColCaption(l_NumLap);

			l_tmp += new string((char)('A'+l_Remainder-1),1);
			return l_tmp;
		}

		protected override void OnRowAdded(RowEventArgs e)
		{
			base.OnRowAdded(e);
			if (e.Row < FixedRows)
				for (int c = FixedCols; c < Cols; c++)
					InsertCell(e.Row,c,CreateCaptionTopCell());
			else
			{
				for (int c = FixedCols; c < Cols; c++)
					InsertCell(e.Row,c,CreateNormalCell());

				for (int c = 0; c < Math.Min(Cols,FixedCols); c++)
					InsertCell(e.Row,c,CreateCaptionLeftCell());
			}
		}
		protected override void OnRowRemoved(RowEventArgs e)
		{
			base.OnRowRemoved(e);

		}
		protected override void OnColumnAdded(ColumnEventArgs e)
		{
			base.OnColumnAdded(e);
			if (e.Column < FixedCols)
				for (int r = FixedRows; r < Rows; r++)
					InsertCell(r,e.Column,CreateCaptionLeftCell());
			else
			{
				for (int r = FixedRows; r < Rows; r++)
					InsertCell(r,e.Column,CreateNormalCell());

				for (int r = 0; r < Math.Min(FixedRows,Rows); r++)
					InsertCell(r,e.Column,CreateCaptionTopCell());
			}
		}
		protected override void OnColumnRemoved(ColumnEventArgs e)
		{
			base.OnColumnRemoved(e);

		}
	}
}

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, along with any associated source code and files, is licensed under The MIT License


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions