Click here to Skip to main content
15,894,343 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 5M   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.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DemoProject
{
	/// <summary>
	/// Summary description for frmPerformance.
	/// </summary>
	public class frmPerformance : System.Windows.Forms.Form
	{
		private SourceGrid.Grid grid1;
		private System.Windows.Forms.Button btLoad;
		private System.Windows.Forms.TextBox txtRow;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.TextBox txtCol;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmPerformance()
		{
			//
			// 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.grid1 = new SourceGrid.Grid();
			this.btLoad = new System.Windows.Forms.Button();
			this.txtRow = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.txtCol = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// grid1
			// 
			this.grid1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
				| System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right);
			this.grid1.BackColor = System.Drawing.Color.White;
			this.grid1.Cols = 0;
			this.grid1.FixedCols = 0;
			this.grid1.FixedRows = 0;
			this.grid1.Location = new System.Drawing.Point(8, 40);
			this.grid1.Name = "grid1";
			this.grid1.Rows = 0;
			this.grid1.Size = new System.Drawing.Size(432, 448);
			this.grid1.TabIndex = 0;
			// 
			// btLoad
			// 
			this.btLoad.Location = new System.Drawing.Point(248, 8);
			this.btLoad.Name = "btLoad";
			this.btLoad.TabIndex = 1;
			this.btLoad.Text = "Load";
			this.btLoad.Click += new System.EventHandler(this.btLoad_Click);
			// 
			// txtRow
			// 
			this.txtRow.Location = new System.Drawing.Point(48, 12);
			this.txtRow.Name = "txtRow";
			this.txtRow.Size = new System.Drawing.Size(64, 20);
			this.txtRow.TabIndex = 2;
			this.txtRow.Text = "500";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 16);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(40, 16);
			this.label1.TabIndex = 3;
			this.label1.Text = "Row";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(120, 16);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(40, 16);
			this.label2.TabIndex = 5;
			this.label2.Text = "Col";
			// 
			// txtCol
			// 
			this.txtCol.Location = new System.Drawing.Point(160, 12);
			this.txtCol.Name = "txtCol";
			this.txtCol.Size = new System.Drawing.Size(64, 20);
			this.txtCol.TabIndex = 4;
			this.txtCol.Text = "200";
			// 
			// frmPerformance
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(448, 491);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.label2,
																		  this.txtCol,
																		  this.label1,
																		  this.txtRow,
																		  this.btLoad,
																		  this.grid1});
			this.Name = "frmPerformance";
			this.Text = "frmPerformance";
			this.ResumeLayout(false);

		}
		#endregion

		private void btLoad_Click(object sender, System.EventArgs e)
		{
			try
			{
				grid1.Redraw = false;

				grid1.EnableRowColSpan = false; //to increase performance

				grid1.Redim(int.Parse(txtRow.Text),int.Parse(txtCol.Text));
				for (int r = 0; r < grid1.Rows;r++)
				{
					for (int c = 0; c < grid1.Cols; c++)
					{
						grid1[r,c] = new SourceGrid.Cell(string.Format("{0},{1}",r.ToString(),c.ToString()));
					}
				}

			}
			catch(Exception err)
			{
				MessageBox.Show(err.Message,"Error");
			}
			finally
			{
				grid1.Redraw = true;
			}
		}
	}
}

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