Click here to Skip to main content
15,896,207 members
Articles / Multimedia / GDI

XPTable - .NET ListView meets Java's JTable

Rate me:
Please Sign up or sign in to vote.
4.97/5 (445 votes)
17 Sep 200512 min read 6.8M   55.3K   888  
A fully customisable ListView style control based on Java's JTable.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using XPTable;
using XPTable.Models;

namespace XPTableDemo.Sorting
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class SortingDemoForm : System.Windows.Forms.Form
	{
		private Table table;
		private ColumnModel columnModel;
		private TableModel tableModel;
		private System.Windows.Forms.CheckBox stableSortCheckBox;
		private System.Windows.Forms.Button sortButton1;
		private System.Windows.Forms.Button sortButton2;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		/// <summary>
		/// 
		/// </summary>
		public SortingDemoForm()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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.table = new XPTable.Models.Table();
			this.columnModel = new XPTable.Models.ColumnModel();
			this.tableModel = new XPTable.Models.TableModel();
			this.stableSortCheckBox = new System.Windows.Forms.CheckBox();
			this.sortButton1 = new System.Windows.Forms.Button();
			this.sortButton2 = new System.Windows.Forms.Button();
			((System.ComponentModel.ISupportInitialize)(this.table)).BeginInit();
			this.SuspendLayout();
			// 
			// table
			// 
			this.table.BackColor = System.Drawing.Color.White;
			this.table.ColumnModel = this.columnModel;
			this.table.EnableHeaderContextMenu = false;
			this.table.HeaderFont = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.table.Location = new System.Drawing.Point(24, 24);
			this.table.Name = "table";
			this.table.Size = new System.Drawing.Size(248, 216);
			this.table.TabIndex = 0;
			this.table.TableModel = this.tableModel;
			this.table.Text = "table1";
			// 
			// stableSortCheckBox
			// 
			this.stableSortCheckBox.Checked = true;
			this.stableSortCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
			this.stableSortCheckBox.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.stableSortCheckBox.Location = new System.Drawing.Point(80, 296);
			this.stableSortCheckBox.Name = "stableSortCheckBox";
			this.stableSortCheckBox.Size = new System.Drawing.Size(144, 16);
			this.stableSortCheckBox.TabIndex = 1;
			this.stableSortCheckBox.Text = "Use stable sort method";
			// 
			// sortButton1
			// 
			this.sortButton1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.sortButton1.Location = new System.Drawing.Point(24, 256);
			this.sortButton1.Name = "sortButton1";
			this.sortButton1.Size = new System.Drawing.Size(112, 23);
			this.sortButton1.TabIndex = 2;
			this.sortButton1.Text = "Sort 1st Column";
			this.sortButton1.Click += new System.EventHandler(this.sortButton1_Click);
			// 
			// sortButton2
			// 
			this.sortButton2.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.sortButton2.Location = new System.Drawing.Point(160, 256);
			this.sortButton2.Name = "sortButton2";
			this.sortButton2.Size = new System.Drawing.Size(112, 23);
			this.sortButton2.TabIndex = 3;
			this.sortButton2.Text = "Sort 2nd Column";
			this.sortButton2.Click += new System.EventHandler(this.sortButton2_Click);
			// 
			// SortingDemoForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(292, 334);
			this.Controls.Add(this.sortButton2);
			this.Controls.Add(this.sortButton1);
			this.Controls.Add(this.stableSortCheckBox);
			this.Controls.Add(this.table);
			this.Name = "SortingDemoForm";
			this.Text = "XPTable - Sorting Demo";
			((System.ComponentModel.ISupportInitialize)(this.table)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new SortingDemoForm());
		}


		/// <summary>
		/// 
		/// </summary>
		/// <param name="e"></param>
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			this.table.BeginUpdate();

			CheckBoxColumn checkBoxColumn = new CheckBoxColumn();
			checkBoxColumn.Text = "CheckBox";
			checkBoxColumn.Width = 94;

			TextColumn textColumn = new TextColumn();
			textColumn.Editable = false;
			textColumn.Text = "Text";

			this.columnModel.Columns.AddRange(new Column[] {checkBoxColumn, textColumn});

			this.table.TableModel.Rows.AddRange(new Row[] {	new Row(new Cell[] {new Cell("CheckBox 0"),
																				   new Cell("Item 1")}),
															  new Row(new Cell[] {new Cell("CheckBox 1"),
																					 new Cell("Item 1")}),
															  new Row(new Cell[] {new Cell("CheckBox 2"),
																					 new Cell("Item 2")}),
															  new Row(new Cell[] {new Cell("CheckBox 3"),
																					 new Cell("Item 2")}),
															  new Row(new Cell[] {new Cell("CheckBox 4"),
																					 new Cell("Item 3")}), 
															  new Row(new Cell[] {new Cell("CheckBox 5"),
																					 new Cell("Item 3")}),
															  new Row(new Cell[] {new Cell("CheckBox 6")}),
															  new Row(new Cell[] {new Cell("CheckBox 7"),
																					 new Cell("Item 4")}),
															  new Row(new Cell[] {new Cell("CheckBox 8"),
																					 new Cell("Item 5")}),
															  new Row(new Cell[] {new Cell("CheckBox 9"),
																					 new Cell("Item 5")})});

			this.table.EndUpdate();
		}

		private void sortButton1_Click(object sender, System.EventArgs e)
		{
			this.table.Sort(0, this.stableSortCheckBox.Checked);
		}

		private void sortButton2_Click(object sender, System.EventArgs e)
		{
			this.table.Sort(1, this.stableSortCheckBox.Checked);
		}

	}
}

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
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions