Click here to Skip to main content
15,886,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 SortableListviewColumnHeader.cs
 * @Purpose A columnheader with support for sorting
 * @Date 19 April 2005, 07:57:06
 * @Author S.Deckers 
 * @Description
 */

namespace Whoua.Src.Sorting
{
	#region -- Using directives --
	using System;
	using System.Collections;
	using System.ComponentModel;
	using System.Drawing;
	using System.Data;
	using System.Windows.Forms;
	//using System.Globalization;
	//using System.Runtime.InteropServices;
	using d = System.Diagnostics.Debug;
	#endregion

	/// <summary date="16-04-2005, 21:04:26" author="S.Deckers">
	/// A columnheader with sorting
	/// </summary>
	class SortableListviewColumnHeader : System.Windows.Forms.ColumnHeader
		, System.Collections.IComparer
	{
		#region -- Properties --
		#region -- Column property --
		/// <summary>
		/// The column to sort
		/// </summary>
		private int _column;
		public int Column
		{
			get{	return( _column);  }
			set{	_column = value;   }
		}
		#endregion

		#region -- ListviewSorter property --
		/// <summary>
		/// The sorter for this column
		/// </summary>
		private ListViewSorter _listviewSorter;
		public ListViewSorter ListviewSorter
		{
			get{	return( _listviewSorter);  }
			set{	_listviewSorter = value;   }
		}
		#endregion
		#endregion

		#region -- PreviousWidth property --
		private int _previousWidth = 0;
		public int PreviousWidth
		{
			get{	return( _previousWidth);  }
			set{	_previousWidth = value;   }
		}
		#endregion

		#region -- LargestSize property --
		/// <summary>
		/// Size largest item
		/// </summary>
		private float _largestSize = -1.00F;
		public float LargestSize
		{
			get{	return( _largestSize);  }
			set{	_largestSize = value;   }
		}
		#endregion

		/// <summary>
		/// Delegate sorting to ListviewSorter
		/// </summary>
		/// <param name="x"></param>
		/// <param name="y"></param>
		/// <returns></returns>
		int System.Collections.IComparer.Compare( object x, object y)
		{
			if( this.ListviewSorter == null)
			{
				throw new System.NotImplementedException( "ListviewSorter is null");
			}
	
			// --- Get items over here ??

			object left  = ((ListViewItem)x).SubItems[ this.Column];
			object right = ((ListViewItem)y).SubItems[ this.Column];

			System.Windows.Forms.ListView listView = this.ListView;
			return( this.ListviewSorter.OnSort( left, right, this.ListView.Sorting));
		}

		/// <summary date="18-04-2005, 07:04:23" author="S.Deckers">
		/// ToString override
		/// </summary>
		/// <returns></returns>
		public override string ToString()
		{
			return( string.Format( "Index={0}, Column={1}, SortOrder={2}, Width={3}, PreviousWidth={4}, LargestWidth={5:F3}",  this.Index, this.Column, this.ListView.Sorting, this.Width, this.PreviousWidth, this.LargestSize));
		}
	}
}

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