Click here to Skip to main content
15,892,199 members
Articles / Multimedia / GDI+

A Bindable, Sortable, Autosizing ListView

Rate me:
Please Sign up or sign in to vote.
4.44/5 (8 votes)
16 May 2005CPOL3 min read 103.7K   1.3K   86  
A listview with support for Databinding, Sorting & Autofit and upon rebinding data reselection of a previous selected item
/**
 * @Name SimpleListviewTest.cs
 * @Purpose A user of the listview
 * @Date 15 May 2005, 16:26:26
 * @Author S.Deckers 
 * @Description The last time I was working on a project where I needed a listview with support
 * for databinding, sorting, and autofit either contents or the columnheader. This article describes
 * the control I came up with
 */

namespace Listview.User
{
	#region -- Using directives --
	using System;
	using System.Drawing;
	using System.Collections;
	using System.ComponentModel;
	using System.Windows.Forms;
	using System.Data;
	#endregion

	/// <summary>
	/// Summary description for SimpleListviewTest.
	/// </summary>
	public class SimpleListviewTest : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private Whoua.Src.bsaListView listView1;
		private System.Windows.Forms.Button button1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public SimpleListviewTest()
		{
			InitializeComponent();
			Init( );
		}

		/// <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.panel1 = new System.Windows.Forms.Panel();
			this.button1 = new System.Windows.Forms.Button();
			this.listView1 = new Whoua.Src.bsaListView();
			this.panel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// panel1
			// 
			this.panel1.Controls.Add(this.button1);
			this.panel1.Dock = System.Windows.Forms.DockStyle.Bottom;
			this.panel1.Location = new System.Drawing.Point(0, 166);
			this.panel1.Name = "panel1";
			this.panel1.Size = new System.Drawing.Size(632, 100);
			this.panel1.TabIndex = 0;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(24, 56);
			this.button1.Name = "button1";
			this.button1.TabIndex = 7;
			this.button1.Text = "Update ds1";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// listView1
			// 
			this.listView1.AllowDrop = true;
			this.listView1.DataTable = null;
			this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.listView1.Location = new System.Drawing.Point(0, 0);
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(632, 166);
			this.listView1.TabIndex = 1;
			// 
			// SimpleListviewTest
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(632, 266);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.panel1);
			this.Name = "SimpleListviewTest";
			this.Text = "SimpleListviewTest";
			this.panel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// Application entry
		/// </summary>
		public static void Main()
		{
			Application.EnableVisualStyles( );
			Application.Run( new SimpleListviewTest( ));
		}

		/// <summary date="15-05-2005, 16:05:52" author="S.Deckers">
		/// Create first dataset and setup databinding for the listview
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void Init( )
		{
			this.listView1.DataTable = CreateDataTable1( );
			this.listView1.DataBind( );
		}

		/// <summary date="15-05-2005, 16:05:16" author="S.Deckers">
		/// Update first dataset en bind listview to it
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void button1_Click(object sender, System.EventArgs e)
		{
			this.listView1.DataTable = this.UpdateData1( this.listView1.DataTable);
			this.listView1.DataBind( );
		}

		/// <summary>
		/// Create first datatable
		/// </summary>
		/// <returns></returns>
		private System.Data.DataTable CreateDataTable1( )
		{
			System.Data.DataTable dataTable = new DataTable( "resultset");    

			dataTable.Columns.Add( new DataColumn( "Date of Birth",	typeof( System.DateTime)));
			dataTable.Columns.Add( new DataColumn( "Name",			typeof( string)));
			dataTable.Columns.Add( new DataColumn( "City",			typeof( string)));
			dataTable.Columns.Add( new DataColumn( "ZIP",			typeof( string)));
			dataTable.Columns.Add( new DataColumn( "Employee #",	typeof( int)));
			dataTable.Columns.Add( new DataColumn( "Weight",		typeof( double)));

			System.Data.DataRow dr = null;

			dr = dataTable.NewRow( );
			dr[ "Date of Birth"] = new System.DateTime( 1937, 8, 12);
			dr[ "Name"] = "Jerry Long";
			dr[ "City"] = "Huntsville";
			dr[ "ZIP"] = "AL 35824";
			dr[ "Employee #"] = 123;
			dr[ "Weight"] = 99.1D;
			dataTable.Rows.Add( dr);

			dr = dataTable.NewRow( );
			dr[ "Date of Birth"] = new System.DateTime( 1955, 12, 1);
			dr[ "Name"] = "Daniel Scales";
			dr[ "City"] = "South-Lebanon";
			dr[ "ZIP"] = "OH 45034";
			dr[ "Employee #"] = 788;
			dr[ "Weight"] = 120.5D;
			dataTable.Rows.Add( dr);

			dr = dataTable.NewRow( );
			dr[ "Date of Birth"] = new System.DateTime( 1966, 2, 14);
			dr[ "Name"] = "Stuard (stu) McDonald";
			dr[ "City"] = System.DBNull.Value;
			dr[ "ZIP"] = "-";
			dr[ "Employee #"] = 1323;
			dr[ "Weight"] = 78.50D;
			dataTable.Rows.Add( dr);
			return( dataTable);
		}

		/// <summary>
		/// Update data from first table
		/// </summary>
		/// <param name="dt"></param>
		/// <returns></returns>
		private System.Data.DataTable UpdateData1( System.Data.DataTable dt)
		{
			if( dt.Rows[ 2][ "City"].ToString() == string.Empty)
			{
				dt.Rows[ 2][ "City"] = "Huntsville/Birmingham AL";
			}
			else
			{
				dt.Rows[ 2][ "City"] = string.Empty;
			}
			return( dt);
		}
	}
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior) Merkator
Netherlands Netherlands
Busy with Intergraph G/Technology-GIS

Comments and Discussions