Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

User Settings Applied

, 5 Sep 2013
Simplifying the .NET User Configuration for Windows Forms and WPF
usersettingsapplied.zip
Configuration
Properties
ConfigurationConsoleDemo
Properties
ConfigurationWindows
Properties
ConfigurationWindowsDemo
App.ico
Properties
ConfigurationWindowsForms
Properties
ConfigurationWindowsFormsDemo
App.ico
ConfigurationWindowsFormsDemo.csproj.user
Properties
usersettingsapplied_scourecode.zip
Configuration2010.csproj.user
ConfigurationWindows2010.csproj.user
App.ico
ConfigurationWindowsDemo2010.csproj.user
App.ico
UserSettingsApplied_v1.1.0.zip
bin
Release
Itenso.Configuration.dll
Itenso.Configuration.Windows.dll
Itenso.Configuration.WindowsForms.dll
obj
Debug
TempPE
Release
TempPE
obj
Debug
TempPE
Release
TempPE
bin
Release
obj
Debug
TempPE
Release
TempPE
App.ico
obj
Debug
TempPE
Release
TempPE
bin
Release
obj
Debug
TempPE
Release
TempPE
App.ico
obj
Debug
TempPE
Release
TempPE
UserSettingsApplied_v1.2.0.zip
Itenso.Configuration.dll
Itenso.Configuration.Windows.dll
Itenso.Configuration.WindowsForms.dll
App.ico
App.ico
// -- 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 name; }
		} // Name

		// ----------------------------------------------------------------------
		public DataGridView DataGridView
		{
			get { return 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 ( dataGridView.Columns.Count == 0 )
				{
					return null;
				}

				List<DataGridViewColumnSetting> columns =
					new List<DataGridViewColumnSetting>( dataGridView.Columns.Count );
				foreach ( DataGridViewColumn dataGridViewColumn in 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 = dataGridView.Columns[ columnSetting.Name ];
					if ( dataGridViewColumn == null )
					{
						continue;
					}

					if ( UseVisibility )
					{
						dataGridViewColumn.Visible = columnSetting.Visible;
					}
					if ( UseWidth )
					{
						dataGridViewColumn.Width = columnSetting.Width;
					}
					if ( 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)

About the Author

Jani Giannoudis
Software Developer (Senior)
Switzerland Switzerland
Jani is Co-founder of Meerazo.com, a free service which allows to share resources like locations, things, persons and their services in a cooperating group of people.

| Advertise | Privacy | Mobile
Web02 | 2.8.140709.1 | Last Updated 5 Sep 2013
Article Copyright 2008 by Jani Giannoudis
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid