Click here to Skip to main content
13,626,866 members
Click here to Skip to main content

Stats

2.9M views
19.6K downloads
993 bookmarked
Posted 19 Jan 2003
Licenced MIT

SourceGrid - Open Source C# Grid Control

, 4 Aug 2013
SourceGrid is a free open source grid control. Supports virtual grid, custom cells and editors, advanced formatting options and many others features
SourceGrid2
References
SourceLibrary.dll
vssver.scc
SampleProject
App.ico
bin
Release
SampleProject.exe
SourceGrid2.dll
SourceLibrary.dll
Extensions
Add.ico
ArrowDown.ICO
ArrowUp.ICO
Delete.ICO
Refresh.ico
vssver.scc
Samples
CalcioSmall.bmp
FACE00.ICO
FACE01.ICO
FACE02.ICO
FACE04.ICO
new.bmp
vssver.scc
SourceGrid
BehaviorModels
vssver.scc
bin
Release
SourceGrid2.dll
SourceLibrary.dll
vssver.scc
Cells
vssver.scc
Common
Icons
CheckBoxChecked.ico
CheckBoxCheckedDisable.ico
CheckBoxCheckedSel.ico
CheckBoxUnChecked.ico
CheckBoxUnCheckedDisable.ico
CheckBoxUnCheckedSel.ico
clear.ico
copy.ico
cut.ico
DeleteCol.ico
DeleteRow.ico
InsertCol.ico
InsertRow.ico
paste.ico
properties.ico
SortDown.ico
SortUp.ico
Thumbs.db
vssver.scc
vssver.scc
DataModels
vssver.scc
Grid.bmp
Grids
vssver.scc
GridVirtual.bmp
SourceGrid2.snk
VisualModels
vssver.scc
SourceGrid
DemoProject
App.ico
bin
Release
DemoProject.exe
SourceGrid.dll
SourceLibrary.dll
FACE00.ICO
FACE01.ICO
FACE02.ICO
FACE04.ICO
new.bmp
PropertyStyleGrid
WorksheetStyleGrid
References
SourceLibrary.dll
SourceGrid
bin
Release
SourceGrid.dll
Cells
Models
PropertyStyleGrid
SourceGrid.snk
TypeCreator
Utility
Chris.Beckett.MenuImageLib
SubItem16.ico
Control
Controls
Icons
CheckBoxChecked.ico
CheckBoxCheckedDisable.ico
CheckBoxCheckedSel.ico
CheckBoxUnChecked.ico
CheckBoxUnCheckedDisable.ico
CheckBoxUnCheckedSel.ico
clear.ico
copy.ico
cut.ico
DeleteCol.ico
DeleteRow.ico
InsertCol.ico
InsertRow.ico
paste.ico
properties.ico
SortDown.ico
SortUp.ico
WorksheetStyleGrid
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

Share

About the Author

Davide Icardi
Software Developer
Italy Italy
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web03-2016 | 2.8.180712.1 | Last Updated 4 Aug 2013
Article Copyright 2003 by Davide Icardi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid