Click here to Skip to main content
12,350,831 members (43,113 online)
Click here to Skip to main content

Stats

339.1K views
6.1K downloads
480 bookmarked
Posted

User Settings Applied

, 5 Sep 2013 CPOL
Simplifying the .NET User Configuration for Windows Forms and WPF
Configuration
Properties
ConfigurationConsoleDemo
Properties
ConfigurationWindows
Properties
ConfigurationWindowsDemo
App.ico
Properties
ConfigurationWindowsForms
Properties
ConfigurationWindowsFormsDemo
App.ico
ConfigurationWindowsFormsDemo.csproj.user
Properties
Configuration2010.csproj.user
ConfigurationWindows2010.csproj.user
App.ico
ConfigurationWindowsDemo2010.csproj.user
App.ico
bin
Release
Itenso.Configuration.dll
Itenso.Configuration.Windows.dll
Itenso.Configuration.WindowsForms.dll
App.ico
App.ico
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)

Share

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.

You may also be interested in...

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