Click here to Skip to main content
15,886,199 members
Articles / Desktop Programming / Windows Forms

User Settings Applied

Rate me:
Please Sign up or sign in to vote.
4.95/5 (136 votes)
5 Sep 2013CPOL10 min read 542.2K   7.5K   489  
Simplifying the .NET User Configuration for Windows Forms and WPF
// -- FILE ------------------------------------------------------------------
// name       : DataGridViewSetting.cs
// created    : Jani Giannoudis - 2008.05.09
// language   : c#
// environment: .NET 2.0
// --------------------------------------------------------------------------
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Configuration;

namespace Itenso.Configuration
{

	// ------------------------------------------------------------------------
	public class DataGridViewSetting : Setting
	{

		// ----------------------------------------------------------------------
		public DataGridViewSetting( DataGridView dataGridView ) :
			this( dataGridView.Name, dataGridView )
		{
		} // DataGridViewSetting

		// ----------------------------------------------------------------------
		public DataGridViewSetting( string name, DataGridView dataGridView )
		{
			if ( string.IsNullOrEmpty( name ) )
			{
				throw new ArgumentNullException( "name" );
			}
			if ( dataGridView == null )
			{
				throw new ArgumentNullException( "dataGridView" );
			}

			this.name = name;
			this.dataGridView = dataGridView;
			UseVisibility = true;
			UseWidth = true;
			UseDisplayIndex = true;
		} // DataGridViewSetting

		// ----------------------------------------------------------------------
		public string Name
		{
			get { return this.name; }
		} // Name

		// ----------------------------------------------------------------------
		public DataGridView DataGridView
		{
			get { return this.dataGridView; }
		} // DataGridView

		// ----------------------------------------------------------------------
		public bool UseVisibility { get; set; }

		// ----------------------------------------------------------------------
		public bool UseWidth { get; set; }

		// ----------------------------------------------------------------------
		public bool UseDisplayIndex { get; set; }

		// ----------------------------------------------------------------------
		public override bool HasChanged
		{
			get
			{
				DataGridViewColumnSetting[] originalColumnSettings = OriginalColumnSettings;
				DataGridViewColumnSetting[] columnSettings = ColumnSettings;
				if ( originalColumnSettings == null || columnSettings == null ||
					originalColumnSettings == columnSettings )
				{
					return false;
				}

				if ( originalColumnSettings.Length != columnSettings.Length )
				{
					return true;
				}

				for ( int i = 0; i < originalColumnSettings.Length; i++ )
				{
					if ( !originalColumnSettings[ i ].Equals( columnSettings[ i ] ) )
					{
						return true;
					}
				}

				return false;
			}
		} // HasChanged

		// ----------------------------------------------------------------------
		private DataGridViewColumnSetting[] OriginalColumnSettings
		{
			get 
			{
				return LoadValue( 
					Name, 
					typeof( DataGridViewColumnSetting[] ),
					SettingsSerializeAs.Binary, 
					null ) as DataGridViewColumnSetting[];
			}
		} // OriginalColumnSettings

		// ----------------------------------------------------------------------
		private DataGridViewColumnSetting[] ColumnSettings
		{
			get
			{
				if ( this.dataGridView.Columns.Count == 0 )
				{
					return null;
				}

				List<DataGridViewColumnSetting> columns =
					new List<DataGridViewColumnSetting>( this.dataGridView.Columns.Count );
				foreach ( DataGridViewColumn dataGridViewColumn in this.dataGridView.Columns )
				{
					columns.Add( new DataGridViewColumnSetting( dataGridViewColumn ) );
				}
				return columns.ToArray();
			}
		} // ColumnSettings

		// ----------------------------------------------------------------------
		public override void Load()
		{
			try
			{
				DataGridViewColumnSetting[] columnSettings = OriginalColumnSettings;
				if ( columnSettings == null || columnSettings.Length == 0 )
				{
					return;
				}

				foreach ( DataGridViewColumnSetting columnSetting in columnSettings )
				{
					DataGridViewColumn dataGridViewColumn = this.dataGridView.Columns[ columnSetting.Name ];
					if ( dataGridViewColumn == null )
					{
						continue;
					}

					if ( this.UseVisibility )
					{
						dataGridViewColumn.Visible = columnSetting.Visible;
					}
					if ( this.UseWidth )
					{
						dataGridViewColumn.Width = columnSetting.Width;
					}
					if ( this.UseDisplayIndex )
					{
						dataGridViewColumn.DisplayIndex = columnSetting.DisplayIndex;
					}
				}
			}
			catch
			{
				if ( ThrowOnErrorLoading )
				{
					throw;
				}
			}
		} // Load

		// ----------------------------------------------------------------------
		public override void Save()
		{
			try
			{
				DataGridViewColumnSetting[] columnSettings = ColumnSettings;
				if ( columnSettings == null )
				{
					return;
				}

				SaveValue( 
					Name,
					typeof( DataGridViewColumnSetting[] ), 
					SettingsSerializeAs.Binary,
					columnSettings,
					null );
			}
			catch
			{
				if ( ThrowOnErrorSaving )
				{
					throw;
				}
			}
		} // Save

		// ----------------------------------------------------------------------
		public override string ToString()
		{
			return string.Concat( name, " (DataGridView)" );
		} // ToString

		// ----------------------------------------------------------------------
		// members
		private readonly DataGridView dataGridView;
		private readonly string name;
	} // class DataGridViewSetting

} // namespace Itenso.Configuration
// -- EOF -------------------------------------------------------------------

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)
Switzerland Switzerland
👨 Senior .NET Software Engineer

🚀 My Open Source Projects
- Time Period Library 👉 GitHub
- Payroll Engine 👉 GitHub

Feedback and contributions are welcome.



Comments and Discussions